513.找树左下角的值 代码随想录
用全局变量追踪当前深度的可能最左叶节点
构建【1,2,3,null,null,null,null】的后三层模型,第二三层null写递的终止条件,第二层和第三二层写叶子节点处理过程,感觉不够时再在上面接上一层,用三角形遍历顺序就行。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int maxDepth = 0;
int res = 0;
public int findBottomLeftValue(TreeNode root) {
res = root.val;
traversal(root , 0);
return res;
}
void traversal(TreeNode root , int depth){
if(root == null){
return;
}
if(root.left == null && root.right == null){
if(depth > maxDepth){//由于先左后右递,depth = maxDepth说明当前depth不是第一个达到最大深度的
res = root.val;
maxDepth = depth;
}
}
if(root.left != null){
depth++;
traversal(root.left , depth);
depth--;
}
if(root.right != null){
depth++;
traversal(root.right , depth);
depth--;
}
}
}
112. 路径总和代码随想录
以上一题为灵感,用全局变量在递下去的同时就做求和,如果匹配到targetSum,就返回1,有一个1,整个递归就返回1,否则返回0,最后检查递归回来的是否是1。
欠卡码思路
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int curSum = 0;
int mark1 = 0 , mark2 = 0;
public boolean hasPathSum(TreeNode root, int targetSum) {
int mark = traversal(root , 0 , targetSum);
return mark == 1;
}
int traversal(TreeNode root , int curSum , int targetSum){
if(root == null){
return 0;
}
curSum = curSum + root.val;
if(root.left == null && root.right == null){
if(curSum == targetSum){
return 1;
}
}
if(root.left != null){
mark1 = traversal(root.left , curSum , targetSum);
}
if(root.right != null){
mark2 = traversal(root.right , curSum , targetSum);
}
if(mark1 == 1 || mark2 == 1){
return 1;
}
return 0;
}
}
106.从中序与后序遍历序列构造二叉树代码随想录
尝试写的时候:
- 无法用数组的长度和特性设置出递的终止条件
- 无论是倒序遍历数组还是顺序遍历数组,?数组顺序遍历同时二叉树递结束开始归好像就可以
- 方法错误,没有意识到中后序结合才能确定一颗二叉树
思路:
- 中序和后序序列可以确定一颗二叉树,反复分割序列从而确定一个个子树
- 在递的同时生成当前子树头结点,并确定递的结束条件(后序数组只有一个时表示叶子结点)
- 自己做不出来就是因为数组看着有规律但不会处理,要后序确定头结点,用头结点切割前序数组
- 把切割的数组递下去
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return traversal(inorder , postorder);
}
TreeNode traversal(int[] inorder, int[] postorder){
if(postorder.length == 0){
return null;
}
if(postorder.length == 1){
TreeNode root = new TreeNode(postorder[0]);
return root;
}
TreeNode root = new TreeNode(postorder[postorder.length - 1]);
int index = 0;
for(int i = 0; i < inorder.length; i++){
if(inorder[i] == postorder[postorder.length - 1]){
index = i;
break;
}
}
int[] leftInorder = arraySub(inorder , 0 , index);
int[] rightInorder = arraySub(inorder , index + 1 , inorder.length);
int[] leftPostorder = arraySub(postorder , 0 , index);
int[] rightPostorder = arraySub(postorder , index , postorder.length - 1);
root.left = traversal(leftInorder , leftPostorder);
root.right = traversal(rightInorder , rightPostorder);
return root;
}
int[] arraySub(int[] array , int a , int b){
int[] temp = new int[b - a];
int j = 0;
for(int i = a; i < b; i++){
temp[j] = array[i];
j++;
}
return temp;
}
}