144. 二叉树的前序遍历
class Solution {
List< Integer> ans = new ArrayList < > ( ) ;
public List< Integer> preorderTraversal ( TreeNode root) {
if ( root == null) return ans;
ans. add ( root. val) ;
preorderTraversal ( root. left) ;
preorderTraversal ( root. right) ;
return ans;
}
}
class Solution {
List< Integer> ans = new ArrayList < > ( ) ;
Stack< TreeNode> s = new Stack < > ( ) ;
public List< Integer> preorderTraversal ( TreeNode root) {
if ( root == null) return ans;
s. push ( root) ;
while ( ! s. empty ( ) ) {
root = s. pop ( ) ;
ans. add ( root. val) ;
if ( null != root. right) s. push ( root. right) ;
if ( null != root. left) s. push ( root. left) ;
}
return ans;
}
}
94. 二叉树的中序遍历
class Solution {
List< Integer> ans = new ArrayList < > ( ) ;
public List< Integer> inorderTraversal ( TreeNode root) {
if ( root == null) return ans;
inorderTraversal ( root. left) ;
ans. add ( root. val) ;
inorderTraversal ( root. right) ;
return ans;
}
}
class Solution {
List< Integer> ans = new ArrayList < > ( ) ;
Stack< TreeNode> s = new Stack < > ( ) ;
public List< Integer> inorderTraversal ( TreeNode root) {
if ( null == root) return ans;
while ( null != root || ! s. empty ( ) ) {
while ( null != root) {
s. push ( root) ;
root = root. left;
}
root = s. pop ( ) ;
ans. add ( root. val) ;
root = root. right;
}
return ans;
}
}
145. 二叉树的后序遍历
class Solution {
List< Integer> ans = new ArrayList < > ( ) ;
public List< Integer> postorderTraversal ( TreeNode root) {
if ( null == root) return ans;
postorderTraversal ( root. left) ;
postorderTraversal ( root. right) ;
ans. add ( root. val) ;
return ans;
}
}
class Solution {
List< Integer> ans = new ArrayList < > ( ) ;
Stack< TreeNode> s1 = new Stack < > ( ) ;
Stack< TreeNode> s2 = new Stack < > ( ) ;
public List< Integer> postorderTraversal ( TreeNode root) {
if ( null == root) return ans;
s1. push ( root) ;
while ( ! s1. empty ( ) ) {
root = s1. pop ( ) ;
s2. push ( root) ;
if ( null != root. left) s1. push ( root. left) ;
if ( null != root. right) s1. push ( root. right) ;
}
while ( ! s2. empty ( ) ) ans. add ( s2. pop ( ) . val) ;
return ans;
}
}
105. 从前序与中序遍历序列构造二叉树
class Solution {
HashMap< Integer, Integer> inorder_v2id = new HashMap < > ( ) ;
public TreeNode search ( int [ ] preorder, int [ ] inorder, int pre_l, int pre_r, int in_l, int in_r) {
if ( pre_l > pre_r || in_l > in_r) return null;
int pre_root_id = pre_l;
int in_root_id = inorder_v2id. get ( preorder[ pre_root_id] ) ;
int left_size = in_root_id - in_l;
TreeNode root = new TreeNode ( preorder[ pre_root_id] ) ;
root. left = search ( preorder, inorder, pre_l + 1 , pre_l + left_size, in_l, in_root_id - 1 ) ;
root. right = search ( preorder, inorder, pre_l + left_size + 1 , pre_r, in_root_id + 1 , in_r) ;
return root;
}
public TreeNode buildTree ( int [ ] preorder, int [ ] inorder) {
int n = preorder. length;
for ( int i = 0 ; i < n; i++ ) {
inorder_v2id. put ( inorder[ i] , i) ;
}
return search ( preorder, inorder, 0 , n - 1 , 0 , n - 1 ) ;
}
}
剑指offer 26. 树的子结构
class Solution {
public boolean isSubStructure ( TreeNode A, TreeNode B) {
return ( A != null && B != null) && ( recur ( A, B) || isSubStructure ( A. left, B) || isSubStructure ( A. right, B) ) ;
}
boolean recur ( TreeNode A, TreeNode B) {
if ( B == null) return true ;
if ( A == null || A. val != B. val) return false ;
return recur ( A. left, B. left) && recur ( A. right, B. right) ;
}
}