leetcode -- 树

题目1. 判断两个树是否相同

题目简介:判断两树是否相同

题解:递归判断左右子树,注意边界条件的判断

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null)return true; //都为空,必然相同
        if(p == null || q == null)return false; //只有一个为空,肯定不同
        return p.val == q.val &&isSameTree(p.left,q.left) &&isSameTree(p.right,q.right);  //节点相同且左子树相同和右子树也相同
        
    }
}

题目2.路径长度

https://leetcode.com/problems/path-sum/

题目简介:从根节点到叶子节点的长度

题解: 判断叶子节点的条件是左右子树都为空且本节点不为空。

/**
 * 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 = sum - root.val;
         if(root != null &&root.left == null && root.right == null){   //叶子节点的表示
             if(sum ==0){
                 return true;
             }else{
                 return false;
             }
        }
       
        return hasPathSum(root.left,sum) || hasPathSum(root.right,sum); //递归判断左右
    }
   
}

题目3.得到所有定长路径(题目二变种升级)

https://leetcode.com/problems/path-sum-ii/

题目简介:得到所有路径

题解:略

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<List<Integer>> totleList = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        dfs(root,sum,new ArrayList<Integer> ());
        return totleList;
    }
    
    void dfs(TreeNode root,int num,List<Integer> list){
        if(root == null)return;
         num -= root.val;
        list.add(root.val);
        
        if(root.left == null && root.right == null && num ==0){
            totleList.add(new ArrayList<Integer>(list));
        }
       
        
        dfs(root.left,num,list);
        dfs(root.right,num,list);
        list.remove(list.size()-1);  //注意遍历过后去掉
    }
}

题目4. 增加树的横向边

https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/

题目简介:给树增加一条横向的边

题解: 广度优先遍历,利用队列。

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}

    public Node(int _val,Node _left,Node _right,Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/
class Solution {
    public Node connect(Node root) {
        if(root == null)return null;
        Deque<Node> queue = new ArrayDeque<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size(); //队列的长度
            Node temp = null;
            for(int i=0;i<size;i++){
                Node node = queue.poll();
                if(node.left != null)queue.offer(node.left);
                if(node.right != null)queue.offer(node.right);
                if(temp != null){
                    temp.next = node; //利用temp记录上个节点
                }
                temp = node;
            }
        }
        return root;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值