计算根节点到叶子节点的所组成的数字(1247, 125, 1367)以及叶子节点到根节点组成的数字(7421, 521, 8631),其二叉树树型结构如下

计算从根节点到叶子节点组成的数字,本质上来说就是二叉树的先序便利的变形,只在每次递归遍历过程中,将上一步计算的结果传到下一轮的递归预算中,其代码如下:

 class Tree(object):

     def __init__(self, val):
self.value = '%s' % str(val)
self.value = val
self.lchild = None
self.rchild = None def __str__(self):
returnList = []
returnList.append(self)
for i in len(returnList):
node = returnList[i]
returnList.append(node.lchild)
returnList.append(node.rchild) return str(returnList) def _repr_(self):
return '<N:%s>' % self.value def TreeSum(depth, value, tree):
if not tree.lchild and not tree.rchild:
print '%s value: %s' % (depth * '\t', value)
else:
#import pdb;pdb.set_trace()
if tree.lchild:
print '%sT%s->lchild --->>>' % (depth * '\t', tree.value)
TreeSum(depth+1, value * 10 + tree.lchild.value, tree.lchild)
if tree.rchild:
print '%sT%s->rchild %s' % (depth * '\t', tree.value, '--->>>'.decode().encode('GBK'))
TreeSum(depth+1, value * 10 + tree.rchild.value, tree.rchild) def RTreeSum(depth, value, tree):
if tree.lchild or tree.rchild:
#import pdb;pdb.set_trace()
if tree.lchild:
print '%sT%s->lchild --->>>' % (depth * '\t', tree.lchild.value)
RTreeSum(depth+1, tree.lchild.value * pow(10, depth) + value, tree.lchild)
if tree.rchild:
print '%sT%s->rchild --->>>' % (depth * '\t', tree.rchild.value)
RTreeSum(depth+1, tree.rchild.value * pow(10, depth) + value, tree.rchild)
else:
print '%s value: %s' % (depth * '\t', value) if __name__ == '__main__':
root = Tree(1)
t2 = Tree(2)
t3 = Tree(3)
t4 = Tree(4)
t5 = Tree(5)
t6 = Tree(6)
t7 = Tree(7)
t8 = Tree(8)
root.lchild = t2
root.rchild = t3
t2.lchild = t4
t2.rchild = t5
t3.lchild = None
t3.rchild = t6
t4.lchild = t7
t4.rchild = None
t6.rchild = t8 print 'Tree Sum:'
TreeSum(0, 1, root) print 'Reverse Tree Sum:'
RTreeSum(1, 1, root)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

其中TreeSum是计算跟节点到叶子节点的数字组合, RTreeSum 是计算叶子节点到根节点的组合

输出结果如下:

 Tree Sum:
T1->lchild --->>>
T2->lchild --->>>
T4->lchild --->>>
value: 1247
T2->rchild --->>>
value: 125
T1->rchild --->>>
T3->rchild --->>>
T6->rchild --->>>
value: 1368
Reverse Tree Sum:
T2->lchild --->>>
T4->lchild --->>>
T7->lchild --->>>
value: 7421
T5->rchild --->>>
value: 521
T3->rchild --->>>
T6->rchild --->>>
T8->rchild --->>>
value: 8631

Python实现二叉树的前序遍历、中序遍历的更多相关文章

  1. Python实现二叉树的前序、中序、后序、层次遍历

      有关树的理论部分描述:<数据结构与算法>-4-树与二叉树:   下面代码均基于python实现,包含: 二叉树的前序.中序.后序遍历的递归算法和非递归算法: 层次遍历: 由前序序列.中 ...

  2. Python实现二叉树的非递归中序遍历

    思路: 1. 使用一个栈保存结点(列表实现): 2. 如果结点存在,入栈,然后将当前指针指向左子树,直到为空: 3. 当前结点不存在,则出栈栈顶元素,并把当前指针指向栈顶元素的右子树: 4. 栈不为空 ...

  3. LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium

    要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...

  4. 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树

    出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...

  5. HDU 1710 (二叉树的前序和中序,求后序)

    题目链接 题目大意: 输入二叉树的前序.中序遍历,请输出它的后序遍历 #include <stdio.h> #include <string.h> ; // 长度为n s1 前 ...

  6. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  7. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  8. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  10. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

随机推荐

  1. SGU481 Hero of Our Time

    Description Saratov ACM ICPC teams have a tradition to come together on Halloween and recollect terr ...

  2. What Can I Do With This Major?

    What Can I Do With This Major? Majors Don’t see your major? Accounting Advertising Africana Studies ...

  3. HDU 1003 Max Sum(DP)

    点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...

  4. Java 单链表逆序

    代码: package com.wangzhu.linkedlist; public class LinkedListDemo { /** * @param args */ public static ...

  5. 李洪强漫谈iOS开发[C语言-023]-取余数运算符

  6. *[topcoder]HexagonalBoard

    http://community.topcoder.com/stat?c=problem_statement&pm=12784 真心觉得tc的div1 250不少好题,对我来说比较适合.这道题 ...

  7. *[codility]MaxCounters

    http://codility.com/demo/take-sample-test/maxcounters 简单题.注意要记录两个max,一个是最大值,一个是已经生效的最大值. // you can ...

  8. OpenCV学习笔记:矩阵的掩码操作

    矩阵的掩码操作很简单.其思想是:根据掩码矩阵(也称作核)重新计算图像中每个像素的值.掩码矩阵中的值表示近邻像素值(包括该像素自身的值)对新像素值有多大影响.从数学观点看,我们用自己设置的权值,对像素邻 ...

  9. 内存不足时Android 系统如何Kill进程

    [转]内存不足时Android 系统如何Kill进程 大家其实都或多或少知道,Android系统有自已的任务管理器,当系统内存不足时,系统需要KILL一些进程(应用),以回收一部分资源,来保证系统仍可 ...

  10. 【HDOJ】2037 今年暑假不AC

    qsort排序后DP,水题.注意,数组开大点儿,把时间理解为0~23,开太小会wa. #include <stdio.h> #include <stdlib.h> #defin ...