LeetCode 965. 单值二叉树

965. 单值二叉树

 

【分析】这道题其实遍历一下二叉树就行了,二叉树遍历的方式有递归/迭代/层次三种,正好借此机会复习一下各种遍历方式。

【递归】

先序:

class Solution {

    int val;

    public boolean dfs(TreeNode node){
        if(node == null) return true;
        if(node.val != val) return false;
        return dfs(node.left) && dfs(node.right);
    }

    public boolean isUnivalTree(TreeNode root) {
        val = root.val;
        return dfs(root);
    }
}

中序:

class Solution {

    int val;

    public boolean dfs(TreeNode node){
        if(node == null) return true;
        boolean left = dfs(node.left);
        if(node.val != val) return false;
        return left && dfs(node.right);
    }

    public boolean isUnivalTree(TreeNode root) {
        val = root.val;
        return dfs(root);
    }
}

后序:

class Solution {

    int val;

    public boolean dfs(TreeNode node){
        if(node == null) return true;
        boolean left = dfs(node.left);
        boolean right = dfs(node.right);
        return node.val == val && left && right;
    }

    public boolean isUnivalTree(TreeNode root) {
        val = root.val;
        return dfs(root);
    }
}

【迭代】

先序:

class Solution {
    public boolean isUnivalTree(TreeNode root) {
        int val = root.val;
        Deque<TreeNode> stack = new LinkedList();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode top = stack.poll();
            if(top.val != val) return false;
            if(top.right != null) stack.push(top.right);
            if(top.left != null) stack.push(top.left);
        }
        return true;
    }
}

中序:

class Solution {
    public boolean isUnivalTree(TreeNode root) {
        int val = root.val;
        Deque<TreeNode> stack = new LinkedList();
        stack.push(root);
        Set<TreeNode> visit = new HashSet();
        while(!stack.isEmpty()){
            TreeNode top = stack.poll();
            if(visit.contains(top)){
                if(top.val != val) return false;
            }
            if(top.right != null) stack.push(top.right);
            if(!visit.contains(top)) stack.push(top);
            visit.add(top);
            if(top.left != null) stack.push(top.left);
        }
        return true;
    }
}

后序:

class Solution {
    public boolean isUnivalTree(TreeNode root) {
        int val = root.val;
        Deque<TreeNode> stack = new LinkedList();
        stack.push(root);
        Set<TreeNode> visit = new HashSet();
        while(!stack.isEmpty()){
            TreeNode top = stack.poll();
            if(visit.contains(top)){
                if(top.val != val) return false;
            }
            if(!visit.contains(top)){
                stack.push(top);
                visit.add(top);
            }
            if(top.right != null) stack.push(top.right);
            if(top.left != null) stack.push(top.left);
        }
        return true;
    }
}

其中中序和后序都需要用一个visit来维护节点的输出顺序,只有当节点被访问过一次后才输出出来。 

【层次 BFS】

不带层信息:

class Solution {
    public boolean isUnivalTree(TreeNode root) {
        int val = root.val;
        Deque<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode top = queue.poll();
            if(top.val != val) return false;
            if(top.left != null) queue.offer(top.left);
            if(top.right != null) queue.offer(top.right);
        }
        return true;
    }
}

带层信息:

class Solution {
    public boolean isUnivalTree(TreeNode root) {
        int val = root.val;
        Deque<TreeNode> queue = new LinkedList();
        queue.offer(root);
        int d = 0;
        while(!queue.isEmpty()){
            int k = queue.size();
            d++;
            while(k-- > 0){
                TreeNode top = queue.poll();
                if(top.val != val) return false;
                if(top.left != null) queue.offer(top.left);
                if(top.right != null) queue.offer(top.right);
            }
        }
        return true;
    }
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值