LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal
来源:互联网 发布:网络推广专员面试 编辑:程序博客网 时间:2024/06/13 16:25
题目描述
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
解题思路
根据二叉树的先序遍历(中左右)和后续遍历(左中右)恢复原来的二叉树。
1. 递归的方式求解,将该问题划分为子问题去求解,先序遍历的第一个元素是整棵树的根节点,从后序遍历的序列中找到与该元素值相同的位置position,可以得知,后序遍历中位置0到position-1的元素构成左子树,position+1到尾部元素构成右子树;先序遍历中,0+1到position(这里根据中序遍历中左子树元素的个数求解)中的元素构成左子树;position+1到尾部的元素构成右子树,依次类推。
2. 论坛中有人提出非递归的方式。先序遍历按照树的左子树一直往下走,到头,然后是刚刚遍历的左节点中距离叶子节点最近的某个左节点的右节点元素;中序遍历是是从树的最左端向上开始遍历,直到碰到有节点存在右节点。可以利用二者的关系求解。
代码
递归
class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder == null || inorder == null || preorder.length != inorder.length || preorder.length == 0) return null; return buildCore(preorder,0,preorder.length-1,inorder,0,inorder.length-1); } private TreeNode buildCore(int[] preorder,int startPre,int endPre,int[] inorder,int startin,int endin){ if(startPre > endPre || startin > endin) return null; TreeNode root = new TreeNode(preorder[startPre]); int position = -1; for(int i=startin;i<=endin;i++){ if(inorder[i] == preorder[startPre]) position = i; } if(position == -1) return null; root.left = buildCore(preorder,startPre+1,position-startin+startPre,inorder,startin,position-1); root.right = buildCore(preorder,position-startin+startPre+1,endPre,inorder,position+1,endin); return root; }}
遍历的方式求解
class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { if(preorder == null || inorder == null || preorder.length != inorder.length || preorder.length == 0) return null; //return buildCore(preorder,0,preorder.length-1,inorder,0,inorder.length-1); Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode result = new TreeNode(preorder[0]); stack.push(result); int index = 0; for(int i=1;i<preorder.length;i++){ TreeNode cur = stack.peek(); if(stack.peek().val != inorder[index]){//不断添加左节点,直到二者相等,说明左节点到头 cur.left = new TreeNode(preorder[i]); stack.push(cur.left); }else{ //去寻找哪个左节点含有右节点,两者不等或者栈为kong while(stack.size() != 0 && stack.peek().val == inorder[index]){ cur = stack.pop();index++; } if(index < preorder.length){ cur.right = new TreeNode(preorder[i]); stack.push(cur.right); } } } return result; }}
阅读全文
0 0
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode --- 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105.Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal LeetCode
- LeetCode#105. Construct Binary Tree from Preorder and Inorder Traversal
- Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode *** 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 【leetcode】105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode-105.Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode - 105. Construct Binary Tree from Preorder and Inorder Traversal
- [Leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal
- [leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal
- 基于selenium和requests的京东商品信息和评论爬虫
- NOIP2009靶形数独(DFS)
- Linux下CMake 安装教程
- 在字符串中找出第一个只出现一次的字符,例如:输入abaccdeff,则输出b
- 深度学习进阶(一.2)--结合GPU搭建theano貌似成功了,但是测试数据运行失败
- LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal
- 探究webassembly(2):性能探究和实验分析
- UVA 12100 Printer Queue(队列,优先队列)
- volatile
- Selenium3+Python3+Firefox56配置及测试解析
- Spring容器扩展点之BeanPostProcessor
- etcd集群安装
- 适配器模式(结构型设计模式)
- XVII Open Cup named after E.V. Pankratiev. XXI Ural Championship G glassese of solutions