zoukankan      html  css  js  c++  java
  • [leetcode]Same Tree

    简单题,树的递归。

    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            // Start typing your Java solution below
            // DO NOT write main() function
            if (p == null && q == null) return true;
            if (p != null && q != null) {
                if (p.val == q.val &&
                    isSameTree(p.left, q.left) &&
                    isSameTree(p.right, q.right))
                    return true;                
            }
            
            return false;
        }
    }
    

    非递归版本,使用了Queue,有点类似BFS。顺便说下Java里面的Queue真难用,连个empty()都没有,要用LinkedList(继承于Queue)

    参照:http://blog.csdn.net/sunbaigui/article/details/8981275

    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            // Start typing your Java solution below
            // DO NOT write main() function
            LinkedList<TreeNode> left = new LinkedList<TreeNode>();
            LinkedList<TreeNode> right = new LinkedList<TreeNode>();
            
            left.offer(p);
            right.offer(q);
            
            while (left.size() != 0 && right.size() != 0) {
                TreeNode ln = left.poll();
                TreeNode rn = right.poll();
                if (ln == null && rn == null) continue;
                if (ln == null || rn == null) return false;
                if (ln.val != rn.val) return false;
                
                left.offer(ln.left); left.offer(ln.right);
                right.offer(rn.left); right.offer(rn.right);
            }
            
            if (left.size() != 0 || right.size() != 0) return false;
            return true;
        }
    }
    

      

  • 相关阅读:
    JavaScript中的数组
    JavaScript中的对象
    Highcharts中设置x轴为时间的写法
    CSS 选择器(基础)
    DOM节点
    平衡木蜻蜓
    python2.7 qt4
    C语言优先级
    树莓派与stm32通信
    linux下USB转串口配置
  • 原文地址:https://www.cnblogs.com/lautsie/p/3247097.html
Copyright © 2011-2022 走看看