GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Algorithms/src/leetcode/SubtreeAnotherTree.java at master · edyluisrey/Leetcode-Algorithms Expand file tree/ Copy pathSubtreeAnotherTree.javaMore file actions 42 lines (35 loc) · 1003 Bytes / Copy pathSubtreeAnotherTree.javaFile metadata and controls42 lines (35 loc) · 1003 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 package leetcode; public class SubtreeAnotherTree { public static void main(String[] args) { SubtreeAnotherTree test = new SubtreeAnotherTree(); TreeNode s= new TreeNode(3); s.left= new TreeNode(4); s.right= new TreeNode(5); s.left.left = new TreeNode(1); s.left.right= new TreeNode(2); TreeNode t= new TreeNode(4); t.left= new TreeNode(1); t.right= new TreeNode(2); System.out.println(test.isSubtree(s, t)); } public boolean isSubtree(TreeNode s, TreeNode t) { if(s==null) return false; if(t==null) return true; return sameTree(s,t) || isSubtree(s.left,t) || isSubtree(s.right,t); } public boolean sameTree(TreeNode s, TreeNode t){ if(s==null && t==null) return true; if(s==null || t==null) return s==t; if(s.val!=t.val) return false; return sameTree(s.left, t.left) && sameTree(s.right, t.right); } } /*class TreeNode{ * int val; * TreeNode left; * TreeNode right; * TreeNode(int x){ * val=x; * } * } * */ |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |