Check if tree b is part of tree a JAVA 实现
2016-09-16 02:01
295 查看
//Coding question: compare two binary trees and find if tree B is a part of tree A. Class TreeNode{ int val; TreeNode left, right; TreeNode(int val){ this.val = val this.left = null; this.right = null; } } public isIdentical(TreeNode A, TreeNode B){ if (B == null){ return true; }else{ return false; } return A.val == B.val && isIdentical(A.left, B.left) && isIdentical(A.rigth, B.right); } public boolean isPartOfTree(TreeNode A, TreeNode B){ if (B == null){ return true; } if (A == null){ return false; } if (isIdentical(A, B)){ return true; } return isPartOfTree(A.left, B) || isPartOfTree(A.right, B); } // if B is null true; isPartOfTree(TreeNode root, null) true; // if A is null false; // root 1 left 2 right 3
相关文章推荐
- How To: Check if a user is part of a SharePoint group in InfoPath
- Check if a binary tree is subtree of another binary tree
- Write an Efficient Method to Check if a Number is Multiple of 3
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- 【LeetCode-面试算法经典-Java实现】【104-Maximum Depth of Binary Tree(二叉树的最大深度)】
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
- Check if the given binary tree is BST or not.
- 【LeetCode-面试算法经典-Java实现】【111-Minimum Depth of Binary Tree(二叉树的最小深度)】
- Check if the key is composed of an arbitrary number of concatenations of strings from the dictionar
- 异常:System.InvalidOperationException: This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms 这个实现是不是Windows平台FIPS验证的加密算法。解决方法
- 【Leetcode】Given a binary tree, check whether it is a mirror of itself
- Check if a given Binary Tree is SumTree
- ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one
- Lowest Common Ancestor of a Binary Tree | Java最短代码实现
- LeetCode题目笔记(四) -- Check if a binary tree is balanced
- How to check if a Number is Positive or Negative in Java - Interview Question
- Write an Efficient Method to Check if a Number is Multiple of 3(快速判断3的倍数)
- Data Structure Binary Tree: Check if a given Binary Tree is SumTree
- This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms. 此实现不是 Windows 平台 FIPS 验证的加密算法的一部分 解决方案