Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

For example, given

  1. inorder = [9,3,15,20,7]
  2. postorder = [9,15,7,20,3]

Return the following binary tree:

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

这个题目思路跟[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal一样, 只是root是postorder[-1], 而inorder[0] 而已, 本质一样.

Code:

  1. class Solution:
  2. def buildTree(self, postorder, inorder):
  3. if not postorder or not inorder: return
  4. root, index = TreeNode(postorder[-1]), inorder.index(postorder[-1])
  5. root.left = self.buildTree(postorder[:index], inorder[:index])
  6. root.right = self.buildTree(postorder[index: -1], inorder[index+1:])
  7. return root

[LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal的更多相关文章

  1. [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal

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

  2. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

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

  3. [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

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

  4. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

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

  5. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

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

  6. Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...

  7. [leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

    原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树. 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左. class Solution ...

  8. leetcode 106 Construct Binary Tree from Inorder and Postorder Traversal----- java

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

  9. C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

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

随机推荐

  1. day_5.27py

    生成器:send()  next() send 和next都可以把生成器向下走,但是send可以传入个参数 ''' 周末继续py 下周回学校过郭星辰生日,还得回来再复查一下 2018-5-27 16: ...

  2. rx.js 的冷和热观察

    http://cn.rx.js.org/manual/overview.html#h213 https://rxjs-cn.github.io/rxjs5-ultimate-cn/content/ho ...

  3. iBatis System.ArgumentNullException : 值不能为 null。 参数名: path2

    System.ArgumentNullException : 值不能为 null. 参数名: path2 在app.config 或 web.config 中加上配置就可以了 <appSetti ...

  4. 证书文件(pfx)读取时报 “指定的网络密码不正确”

    实际情况: 1.本地测试正确,发布到windows server 2003 iis6 可以正常运行 发布到 windows server 2008 上 II7就报 “指定的网络密码不正确” 日志报错为 ...

  5. role="navigation"

    HTML5的标签属性,可以用于标识一个普通的标签,使之语义化,方便浏览器对其具体功能进行识别. 例如div容器制作的导航栏,加上role="navigation",就可以让浏览器知 ...

  6. 创建结点 与 分配内存 Function to create a Node. Allocates memory for a new node. 主动申请内存 链表 指针的写法

    Self Referential Data Structure in C - create a singly linked list http://www.how2lab.com/programmin ...

  7. Chap7:民间用语[《区块链中文词典》维京&甲子]

  8. [development][tcp/ip][ids] 一个简单有参考价值的库 libnids

    libhtp 中的例子, 可以通过libnids快速使用. 或者可以快速的写个sniffer. 支持三个功能 ip分片重组, tcp乱序重排, 端口扫描发现. 工程: https://github.c ...

  9. 在RAC执行相关操作发生ora-01031:insufficient privileges解决方法

    grid用户下 寻找命令的路径 如which crsctl su - root用户 然后根据找到的路径去执行命令 如 /u01/app/oracle/product/11.2.0/db_1/bin/c ...

  10. linux关闭的时候出现异常: java.net.ConnectException: 拒绝连接 (Connection refused)

    这个时候: 需要先把java进程杀死. 然后再重新启动. 杀死进程: ps -aux | grep java 然后kill -9 进程 然后再重新启动.