diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index 17ba561d..94eb405b 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -34,7 +34,7 @@ ## 思路 -首先回忆一下如何根据两个顺序构造一个唯一的二叉树,相信理论知识大家应该都清楚,就是以 后序数组的最后一个元素为切割点,先切中序数组,根据中序数组,反过来在切后序数组。一层一层切下去,每次后序数组最后一个元素就是节点元素。 +首先回忆一下如何根据两个顺序构造一个唯一的二叉树,相信理论知识大家应该都清楚,就是以 后序数组的最后一个元素为切割点,先切中序数组,根据中序数组,反过来再切后序数组。一层一层切下去,每次后序数组最后一个元素就是节点元素。 如果让我们肉眼看两个序列,画一棵二叉树的话,应该分分钟都可以画出来。 @@ -236,7 +236,7 @@ private: vector leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size()); vector rightPostorder(postorder.begin() + leftInorder.size(), postorder.end()); - // 一下为日志 + // 以下为日志 cout << "----------" << endl; cout << "leftInorder :"; @@ -275,7 +275,7 @@ public: }; ``` -**此时应该发现了,如上的代码性能并不好,应为每层递归定定义了新的vector(就是数组),既耗时又耗空间,但上面的代码是最好理解的,为了方便读者理解,所以用如上的代码来讲解。** +**此时应该发现了,如上的代码性能并不好,因为每层递归定义了新的vector(就是数组),既耗时又耗空间,但上面的代码是最好理解的,为了方便读者理解,所以用如上的代码来讲解。** 下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割) @@ -569,7 +569,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。 之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。 -所以要避免眼高手低,踏实的把代码写出来。 +所以要避免眼高手低,踏实地把代码写出来。 我同时给出了添加日志的代码版本,因为这种题目是不太容易写出来调一调就能过的,所以一定要把流程日志打出来,看看符不符合自己的思路。 @@ -728,25 +728,33 @@ class Solution: * Right *TreeNode * } */ +var ( + hash map[int]int +) func buildTree(inorder []int, postorder []int) *TreeNode { - if len(inorder)<1||len(postorder)<1{return nil} - //先找到根节点(后续遍历的最后一个就是根节点) - nodeValue:=postorder[len(postorder)-1] - //从中序遍历中找到一分为二的点,左边为左子树,右边为右子树 - left:=findRootIndex(inorder,nodeValue) - //构造root - root:=&TreeNode{Val: nodeValue, - Left: buildTree(inorder[:left],postorder[:left]),//将后续遍历一分为二,左边为左子树,右边为右子树 - Right: buildTree(inorder[left+1:],postorder[left:len(postorder)-1])} + hash = make(map[int]int) + for i, v := range inorder { // 用map保存中序序列的数值对应位置 + hash[v] = i + } + // 以左闭右闭的原则进行切分 + root := rebuild(inorder, postorder, len(postorder)-1, 0, len(inorder)-1) return root } -func findRootIndex(inorder []int,target int) (index int){ - for i:=0;i r { // 说明没有元素,返回空树 + return nil } - return -1 + if l == r { // 只剩唯一一个元素,直接返回 + return &TreeNode{Val : inorder[l]} + } + rootV := postorder[rootIdx] // 根据后序数组找到根节点的值 + rootIn := hash[rootV] // 找到根节点在对应的中序数组中的位置 + root := &TreeNode{Val : rootV} // 构造根节点 + // 重建左节点和右节点 + root.Left = rebuild(inorder, postorder, rootIdx-(r-rootIn)-1, l, rootIn-1) + root.Right = rebuild(inorder, postorder, rootIdx-1, rootIn+1, r) + return root } ``` @@ -761,22 +769,27 @@ func findRootIndex(inorder []int,target int) (index int){ * Right *TreeNode * } */ +var ( + hash map[int]int +) func buildTree(preorder []int, inorder []int) *TreeNode { - if len(preorder)<1||len(inorder)<1{return nil} - left:=findRootIndex(preorder[0],inorder) - root:=&TreeNode{ - Val: preorder[0], - Left: buildTree(preorder[1:left+1],inorder[:left]), - Right: buildTree(preorder[left+1:],inorder[left+1:])} + hash = make(map[int]int, len(inorder)) + for i, v := range inorder { + hash[v] = i + } + root := build(preorder, inorder, 0, 0, len(inorder)-1) // l, r 表示构造的树在中序遍历数组中的范围 return root } -func findRootIndex(target int,inorder []int) int{ - for i:=0;i r { + return nil } - return -1 + rootVal := pre[root] // 找到本次构造的树的根节点 + index := hash[rootVal] // 根节点在中序数组中的位置 + node := &TreeNode {Val: rootVal} + node.Left = build(pre, in, root + 1, l, index-1) + node.Right = build(pre, in, root + (index-l) + 1, index+1, r) + return node } ``` diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index d50f23f9..cb9d343f 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -33,7 +33,7 @@ * 112.路径总和 * 113.路径总和ii -这道题我们要遍历从根节点到叶子节点的的路径看看总和是不是目标和。 +这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。 ### 递归 @@ -167,7 +167,7 @@ public: }; ``` -**是不是发现精简之后的代码,已经完全看不出分析的过程了,所以我们要把题目分析清楚之后,在追求代码精简。** 这一点我已经强调很多次了! +**是不是发现精简之后的代码,已经完全看不出分析的过程了,所以我们要把题目分析清楚之后,再追求代码精简。** 这一点我已经强调很多次了! ### 迭代 @@ -316,13 +316,13 @@ class solution { } if (root.left != null) { boolean left = haspathsum(root.left, targetsum); - if (left) {// 已经找到 + if (left) { // 已经找到 return true; } } if (root.right != null) { boolean right = haspathsum(root.right, targetsum); - if (right) {// 已经找到 + if (right) { // 已经找到 return true; } } @@ -348,31 +348,37 @@ class solution { ```java class solution { public boolean haspathsum(treenode root, int targetsum) { - if(root==null)return false; + if(root == null) return false; stack stack1 = new stack<>(); stack stack2 = new stack<>(); - stack1.push(root);stack2.push(root.val); - while(!stack1.isempty()){ + stack1.push(root); + stack2.push(root.val); + while(!stack1.isempty()) { int size = stack1.size(); - for(int i=0;i0{ - length:=queue.Len() - for i:=0;imaxDeep{ - value=root.Val - maxDeep=deep - } - } - //递归 - if root.Left!=nil{ - deep++ - findLeftValue(root.Left,deep) - deep--//回溯 + +func dfs(root *TreeNode, d int) { + if root == nil { + return } - if root.Right!=nil{ - deep++ - findLeftValue(root.Right,deep) - deep--//回溯 + // 因为先遍历左边,所以左边如果有值,右边的同层不会更新结果 + if root.Left == nil && root.Right == nil && depth < d { + depth = d + res = root.Val } + dfs(root.Left, d+1) // 隐藏回溯 + dfs(root.Right, d+1) } ``` @@ -358,18 +349,21 @@ func findLeftValue (root *TreeNode,deep int){ ```go func findBottomLeftValue(root *TreeNode) int { - queue:=list.New() var gradation int + queue := list.New() + queue.PushBack(root) - for queue.Len()>0{ - length:=queue.Len() - for i:=0;i 0 { + length := queue.Len() + for i := 0; i < length; i++ { + node := queue.Remove(queue.Front()).(*TreeNode) + if i == 0 { + gradation = node.Val + } + if node.Left != nil { queue.PushBack(node.Left) } - if node.Right!=nil{ + if node.Right != nil { queue.PushBack(node.Right) } } @@ -385,19 +379,18 @@ func findBottomLeftValue(root *TreeNode) int { ```javascript var findBottomLeftValue = function(root) { //首先考虑递归遍历 前序遍历 找到最大深度的叶子节点即可 - let maxPath = 0,resNode = null; + let maxPath = 0, resNode = null; // 1. 确定递归函数的函数参数 - const dfsTree = function(node,curPath){ + const dfsTree = function(node, curPath) { // 2. 确定递归函数终止条件 - if(node.left===null&&node.right===null){ - if(curPath>maxPath){ + if(node.left === null && node.right === null) { + if(curPath > maxPath) { maxPath = curPath; resNode = node.val; } - // return ; } - node.left&&dfsTree(node.left,curPath+1); - node.right&&dfsTree(node.right,curPath+1); + node.left && dfsTree(node.left, curPath+1); + node.right && dfsTree(node.right, curPath+1); } dfsTree(root,1); return resNode; @@ -409,20 +402,20 @@ var findBottomLeftValue = function(root) { var findBottomLeftValue = function(root) { //考虑层序遍历 记录最后一行的第一个节点 let queue = []; - if(root===null){ + if(root === null) { return null; } queue.push(root); let resNode; - while(queue.length){ - let length = queue.length; - for(let i=0; i