root ,翻转这棵二叉树,并返回其根节点。需要遍历整颗二叉树,每个节点的左右子节点进行一次交换就行
-
- public TreeNode invertTree(TreeNode root) {
- search(root);
- return root;
-
- }
- public void search(TreeNode root){
- if(root == null)
- return root;
- invertTree(root.left);
- invertTree(root.right);
- TreeNode temp;
- temp = root.left;
- root.left = root.right;
- root.right = temp;
- }
root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。- class Solution {
- public List
binaryTreePaths(TreeNode root) { - List
res = new ArrayList<>(); - if (root == null) {
- return res;
- }
- List
paths = new ArrayList<>(); - traversal(root, paths, res);
- return res;
- }
-
- private void traversal(TreeNode root, List
paths, List res ) { - paths.add(root.val);
- // 叶子结点
- if (root.left == null && root.right == null) {
- // 输出
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < paths.size() - 1; i++) {
- sb.append(paths.get(i)).append("->");
- }
- sb.append(paths.get(paths.size() - 1));
- res.add(sb.toString());
- return;
- }
- if (root.left != null) {
- traversal(root.left, paths, res);
- paths.remove(paths.size() - 1);// 回溯
- }
- if (root.right != null) {
- traversal(root.right, paths, res);
- paths.remove(paths.size() - 1);// 回溯
- }
- }
- }
【3】

- //##########递归模板##########
- // 接住返回值
- 左返回值 = search(root.left);
- 右返回值 = search(root.right);
- 逻辑处理左右返回值+自身节点逻辑处理 返回
-
- 主要就是逻辑处理,
- 左子树,右子树搜索到的结果直接返回么?
- 或者考虑两者关系后直接返回?
- 或者考虑父节点和自身关系返回?
root , 检查它是否轴对称。- class Solution {
- public boolean isSymmetric(TreeNode root) {
- return check(root, root);
- }
-
- public boolean check(TreeNode p, TreeNode q) {
- if (p == null && q == null) {
- return true;
- }
- if (p == null || q == null) {
- return false;
- }
- boolean left = check(p.left, q.right);
- boolean right = check(p.right, q.left);
- return p.val == q.val && left && right ;
- }
- }
- class Solution {
- public int maxDepth(TreeNode root) {
- if(root == null)
- return 0;
- int leftDepth = maxDepth(root.left);
- int rightDepth = maxDepth(root.right);
- return (leftDepth>rightDepth?leftDepth:rightDepth) + 1;
- }
- }
- class Solution {
- public int minDepth(TreeNode root) {
- if(root == null)
- return 0;
-
- int leftDepth = minDepth(root.left);
- int rightDepth = minDepth(root.right);
- if(leftDepth == 0)
- return rightDepth+1;
- if(rightDepth==0)
- return leftDepth+1;
- return Math.min(leftDepth,rightDepth)+1;
- }
- }
- class Solution {
- public boolean isBalanced(TreeNode root) {
- return depth(root) != -1;
- }
- public int depth(TreeNode root){
- if(root == null)
- return 0;
- int l_Depth = depth(root.left);
- if(l_Depth == -1)
- return -1;
- int r_Depth = depth(root.right);
- if(r_Depth == -1)
- return -1;
- if(Math.abs(l_Depth-r_Depth)>1)
- return -1;
- return Math.max(l_Depth,r_Depth) +1;
- }
- }
root ,返回所有左叶子之和。
- class Solution {
- public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
- if (root == null || root == p || root == q) { // 递归结束条件
- return root;
- }
-
- // 后序遍历
- TreeNode left = lowestCommonAncestor(root.left, p, q);
- TreeNode right = lowestCommonAncestor(root.right, p, q);
-
- if(left == null && right == null) { // 若未找到节点 p 或 q
- return null;
- }else if(left == null && right != null) { // 若找到一个节点
- return right;
- }else if(left != null && right == null) { // 若找到一个节点
- return left;
- }else { // 若找到两个节点
- return root;
- }
- }
- }
- //=====代码模板====
- if(search(root.left))
- return;
- if(search(root.right))
- return;
- class solution {
- public boolean haspathsum(treenode root, int targetsum) {
- if (root == null) {
- return false;
- }
- targetsum -= root.val;
- // 叶子结点
- if (root.left == null && root.right == null) {
- return targetsum == 0;
- }
- if (root.left != null) {
- boolean left = haspathsum(root.left, targetsum);
- if (left) {// 已经找到
- return true;
- }
- }
- if (root.right != null) {
- boolean right = haspathsum(root.right, targetsum);
- if (right) {// 已经找到
- return true;
- }
- }
- return false;
- }
- }
- class Solution {
- // 递归,利用二叉搜索树特点,优化
- public TreeNode searchBST(TreeNode root, int val) {
- if (root == null || root.val == val) {
- return root;
- }
- if (val < root.val) {
- return searchBST(root.left, val);
- } else {
- return searchBST(root.right, val);
- }
- }
- }
-
- class Solution {
- public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
- if (root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
- if (root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q);
- return root;
- }
- }
- class Solution {
- Map
map; // 方便根据数值查找位置 - public TreeNode buildTree(int[] inorder, int[] postorder) {
- map = new HashMap<>();
- for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
- map.put(inorder[i], i);
- }
-
- return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开
- }
-
- public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
- // 参数里的范围都是前闭后开
- if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树
- return null;
- }
- int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
- TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
- int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
- root.left = findNode(inorder, inBegin, rootIndex,
- postorder, postBegin, postBegin + lenOfLeft);
- root.right = findNode(inorder, rootIndex + 1, inEnd,
- postorder, postBegin + lenOfLeft, postEnd - 1);
-
- return root;
- }
- }
- class Solution {
- Map
map; - public TreeNode buildTree(int[] preorder, int[] inorder) {
- map = new HashMap<>();
- for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
- map.put(inorder[i], i);
- }
-
- return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开
- }
-
- public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
- // 参数里的范围都是前闭后开
- if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树
- return null;
- }
- int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置
- TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
- int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数
- root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
- inorder, inBegin, rootIndex);
- root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
- inorder, rootIndex + 1, inEnd);
-
- return root;
- }
- }
- class Solution {
- public TreeNode constructMaximumBinaryTree(int[] nums) {
- return constructMaximumBinaryTree1(nums, 0, nums.length);
- }
-
- public TreeNode constructMaximumBinaryTree1(int[] nums, int leftIndex, int rightIndex) {
- if (rightIndex - leftIndex < 1) {// 没有元素了
- return null;
- }
- if (rightIndex - leftIndex == 1) {// 只有一个元素
- return new TreeNode(nums[leftIndex]);
- }
- int maxIndex = leftIndex;// 最大值所在位置
- int maxVal = nums[maxIndex];// 最大值
- for (int i = leftIndex + 1; i < rightIndex; i++) {
- if (nums[i] > maxVal){
- maxVal = nums[i];
- maxIndex = i;
- }
- }
- TreeNode root = new TreeNode(maxVal);
- // 根据maxIndex划分左右子树
- root.left = constructMaximumBinaryTree1(nums, leftIndex, maxIndex);
- root.right = constructMaximumBinaryTree1(nums, maxIndex + 1, rightIndex);
- return root;
- }
- }
- class Solution {
- public TreeNode sortedArrayToBST(int[] nums) {
- return travesal(nums,0,nums.length );
- }
- public TreeNode travesal(int[] nums, int left,int right){
- if(left >= right)
- return null;
- if(right - left == 1)
- return new TreeNode(nums[left]);
- int mid = left + (right - left) / 2;
- TreeNode root = new TreeNode(nums[mid]);
- root.left = travesal(nums,left,mid);
- root.right = travesal(nums,mid+1,right);
- return root;
- }
- }
- //#############模板##############
- root.left = search(root.left);
- root.right = search(root.right)
- 递归时候确保每个节点的修树改树正确
- class Solution {
- public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
- if(root1 == null && root2 == null)
- return null;
- if(root1 == null)
- return root2;
- if(root2 == null)
- return root1;
- root1.val += root2.val;
- root1.left = mergeTrees(root1.left,root2.left);
- root1.right = mergeTrees(root1.right,root2.right);
- return root1;
- }
- }
- class Solution {
- public TreeNode insertIntoBST(TreeNode root, int val) {
- if (root == null) // 如果当前节点为空,也就意味着val找到了合适的位置,此时创建节点直接返回。
- return new TreeNode(val);
-
- if (root.val < val){
- root.right = insertIntoBST(root.right, val); // 递归创建右子树
- }else if (root.val > val){
- root.left = insertIntoBST(root.left, val); // 递归创建左子树
- }
- return root;
- }
- }
-
-
- class Solution {
- public TreeNode deleteNode(TreeNode root, int key) {
- root = delete(root,key);
- return root;
- }
-
- private TreeNode delete(TreeNode root, int key) {
- if (root == null) return null;
-
- if (root.val > key) {
- root.left = delete(root.left,key);
- } else if (root.val < key) {
- root.right = delete(root.right,key);
- } else {
- if (root.left == null) return root.right;
- if (root.right == null) return root.left;
- TreeNode tmp = root.right;
- while (tmp.left != null) {
- tmp = tmp.left;
- }
- root.val = tmp.val;
- root.right = delete(root.right,tmp.val);
- }
- return root;
- }
- }
- class Solution {
- public TreeNode trimBST(TreeNode root, int low, int high) {
- if (root == null) {
- return null;
- }
- if (root.val < low) {
- return trimBST(root.right, low, high);
- }
- if (root.val > high) {
- return trimBST(root.left, low, high);
- }
- // root在[low,high]范围内
- root.left = trimBST(root.left, low, high);
- root.right = trimBST(root.right, low, high);
- return root;
- }
- }
-
- private void findLeftValue (TreeNode root,int deep) {
- if (root == null) return;
- if (root.left == null && root.right == null) {
- if (deep > Deep) {
- value = root.val;
- Deep = deep;
- }
- }
- if (root.left != null) findLeftValue(root.left,deep + 1);
- if (root.right != null) findLeftValue(root.right,deep + 1);
- }
- class Solution {
- public int sumOfLeftLeaves(TreeNode root) {
- return sum(root,false);
- }
- public int sum(TreeNode root, boolean flag){
- if(root == null)
- return 0;
- if(root.left == null && root.right ==null && flag)
- return root.val;
- return sum(root.left,true) + sum(root.right,false);
- }
- }