【LeetCode从零单排】No100 Same Tree No101 Symmetric 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) {if(p==null && q==null) return true;if(p==null || q==null || q.val!=p.val ) return false;if(isSameTree(p.left,q.left)==false || isSameTree(p.right,q.right)==false) return false;return true;}}

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

代码下载:https://github.com/jimenbian/GarvinLeetCode

/********************************

* 本文来自博客 “李博Garvin“

* 转载请标明出处:

******************************************/

,销售世界上第一号的产品——不是汽车,

【LeetCode从零单排】No100 Same Tree No101 Symmetric Tree

相关文章:

你感兴趣的文章:

标签云: