leetcode总结——树篇

自上而下:
路径和sum
最短路径
镜像树

层次遍历
树化链表
完全二叉树的节点个数:

二叉搜索树的个数
二叉搜索树的生成
是否二叉搜索树
验证先序遍历二叉线索树
右侧二叉树
前缀树
排序树中第k大的值
二叉搜索树的最近公共祖先
同值二叉树:

最长连续路径
垂直遍历二叉树
最小高度树
序列化二叉树

路径和sum:
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。

说明: 叶子节点是指没有子节点的节点。

示例:
给定如下二叉树,以及目标和 sum = 22,

          5
         / \
        4   8
       /   / \
      11  13  4
     /  \      \
    7    2      1

返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        if(root==null) return false;
        sum-=root.val;
        if(root.left==null&&root.right==null)return(sum==0);
        return hasPathSum(root.left,sum)||hasPathSum(root.right,sum);
    }
}

时间复杂度: O(N)
空间复杂度:最坏情况下,整棵树是非平衡的,例如每个节点都只有一个孩子,递归会调用 N次(树的高度),因此栈的空间开销是 O(N) 。但在最好情况下,树是完全平衡的,高度只有 log(N),因此在这种情况下空间复杂度只有 O(log(N)) 。

总结:树的递归调用,习惯用return后接递归函数,使得代码简洁

最短路径
题目一:
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回它的最小深度 2.

答案一:
首先分析错误答案:

import java.lang.*;
class Solution {
    public int minDepth(TreeNode root) {
       if(root==null){
           return 0;
       }
       return Min(minDepth(root.left)+1,minDepth(root.right)+1)l
    }
}

此答案的错误点在于,必须是根节点到叶子节点
即若在树种出现度为1的点,则长度应该是
minDepth(root.left)==null?minDepth(root.right):minDepth(root.left))

正确答案:
当 root 节点左右孩子都为空时,返回 1
当 root 节点左右孩子有一个为空时,返回不为空的孩子节点的深度
当 root 节点左右孩子都不为空时,返回左右孩子较小深度的节点值

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.lang.*;
class Solution {
    public int minDepth(TreeNode root) {
       if(root==null){
           return 0;
       } 
       if(root.left==null&&root.right==null){
           return 1;
       }
       int  m1=minDepth(root.left);
       int  m2=minDepth(root.right);
       return root.left==null||root.right==null?m1+m2+1:Math.min(m1,m2)+1;        
    }
}

镜像树
给定一个二叉树,检查它是否是镜像对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。
1
/
2 2
/ \ /
3 4 4 3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:
1
/
2 2
\
3 3

利用递归的方法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
           return mirror(root,root);
    }
    public boolean mirror(TreeNode root1,TreeNode root2){
        if(root1==null&&root2==null)return true;
        if(root1==null||root2==null)return false;
        return (root1.val==root2.val)&&mirror(root1.left,root2.right)&&mirror(root1.right,root2.left);
    }
        
}

层次遍历
leetcode 102
题目二:
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
例如:
给定二叉树: [3,9,20,null,null,15,7]
3
/
9 20
/
15 7
返回其层次遍历结果:

[
[3],
[9,20],
[15,7]
]

迭代:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.*;
import java.lang.*;
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list=new  LinkedList<>();
        if(root==null) return list;
        Queue<TreeNode> q=new LinkedList<>();
        q.add(root);
        int height=0;
        while(!q.isEmpty()){
           List<Integer> list1=new  LinkedList<>();
            list.add(list1);
            int nums=q.size();
            for(int i=0;i<nums;i++){    
                TreeNode node=q.poll();
                list.get(height).add(node.val);   
                if(node.left!=null)q.add(node.left);
                if(node.right!=null)q.add(node.right);
            }
            height++;
        }
        return list;
    }
}

题目一:
给定一个二叉树,原地将它展开为链表。
例如,给定二叉树

    1
   / \
  2   5
 / \   \
3   4   6

将其展开为:

1
 \
  2
   \
    3
     \
      4
       \
        5
         \
          6

递归方法。

class Solution {
    public void flatten(TreeNode root) {
        changeNode(root);
    }
    
    public void changeNode(TreeNode nod
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值