100. Same Tree

原题链接:https://leetcode.com/problems/same-tree/description/
这道题目本身超级简单,解法就是递归遍历二叉树即可!

/**
 * 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 == q) {
            return true;
        }
        
        if (p == null || q == null) {
            return false;
        }
        
        if (p.val == q.val) {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }

        return false;
    }

posted @ 2018-03-09 22:35  optor  阅读(112)  评论(0编辑  收藏  举报