博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
阅读量:5742 次
发布时间:2019-06-18

本文共 2228 字,大约阅读时间需要 7 分钟。

Construct Binary Tree from Preorder and Inorder Traversal

 

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

Note:

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

SOLUTION 1:

1. Find the root node from the preorder.(it is the first node.)

2. Try to find the position of the root in the inorder. Then we can get the number of nodes in the left tree.

3. 递归调用,构造左子树和右子树。

例子:

Pre:       4 2 1 3 6 5 7

Inorder: 1 2 3 4 5 6 7 

1 /** 2  * Definition for binary tree 3  * public class TreeNode { 4  *     int val; 5  *     TreeNode left; 6  *     TreeNode right; 7  *     TreeNode(int x) { val = x; } 8  * } 9  */10 public class Solution {11     public TreeNode buildTree(int[] preorder, int[] inorder) {12         // bug 3: consider when length is 0.13         if (preorder == null || inorder == null || preorder.length == 0 || preorder.length != inorder.length) {14             return null;15         }16         17         // bug 4: end index is length - 1.18         return buildTree(preorder, inorder, 0, preorder.length - 1, 0, preorder.length - 1);19     }20     21     public TreeNode buildTree(int[] preorder, int[] inorder, int preStart, int preEnd, int inStart, int inEnd) {22         // base case;23         if (preStart > preEnd) {24             return null;25         }26         27         int rootVal = preorder[preStart];28         TreeNode root = new TreeNode(rootVal);29         30         int pos = findTarget(inorder, rootVal, inStart, inEnd);31         32         // bug 5: left number is pos - instart can't add 133         int leftNum = pos - inStart;34         35         root.left = buildTree(preorder, inorder, preStart + 1, preStart + leftNum, inStart, pos - 1);36         root.right = buildTree(preorder, inorder, preStart + leftNum + 1, preEnd, pos + 1, inEnd);37         38         return root;39     }40     41     // bug 1: return type required.42     // bug 2: this is not a bst. can't use binary search.43     public int findTarget(int[] A, int target, int start, int end) {44         for (int i = start; i <= end; i++) {45             if (target == A[i]) {46                 return i;47             }48         }49         50         return -1;51     }52 }
View Code

 

GITHUB:

转载地址:http://tnizx.baihongyu.com/

你可能感兴趣的文章
Javascript String类的属性及方法
查看>>
[LeetCode] Merge Intervals
查看>>
Ubuntu 14.04 vsftp refusing to run with writable root inside chroot问题解决方法
查看>>
Struts2 学习小结
查看>>
烂泥:wordpress迁移到docker
查看>>
测试工具综合
查看>>
asp.net中调用COM组件发布IIS时常见错误 80070005解决方案
查看>>
分享一段ios数据库代码,包括对表的创建、升级、增删查改
查看>>
如何书写高质量的jQuery代码
查看>>
Activity的生命周期整理
查看>>
【记录】JS toUpperCase toLowerCase 大写字母/小写字母转换
查看>>
在 Linux 系统中安装Load Generator ,并在windows 调用
查看>>
Visifire charts ToolBar
查看>>
Mysql查询
查看>>
数据传输流程和socket简单操作
查看>>
ProbS CF matlab源代码(二分系统)(原创作品,转载注明出处,谢谢!)
查看>>
OC中KVC的注意点
查看>>
JQ入门(至回调函数)
查看>>
【洛天依】几首歌的翻唱(无伴奏)
查看>>
OpenSSL初瞻及本系列的博文的缘由
查看>>