A binary tree is a type of tree structure with at most two child nodes. Here will list some common operations and my solution, the specific introduction and topic are in this LeetCode .
- Traversal
1. Pre-order traversal
2. In-order traversal
3. Post-order traversal
4. Layer-order traversal - Recursion problem
1. The depth of the tree
2. The mirror tree
3. Whether there is a path to the leaf node
4. The binary tree is obtained from the in-order and the post-order
5. The binary tree is obtained from the pre-order and in-order
6. The nearest parent node - Other
1. Hierarchical links
traverse
There are four ways of traversal. In addition to the common pre-order, in-order, and post-order, there is also a layer-order traversal.
Pre-order traversal: middle left and right
In- order traversal: left middle right
Post-order traversal: left and right middle
It can be seen that the front, middle, and back represent the location of the parent node.
Layer-order traversal is a layer-by-layer traversal from left to right.
preorder traversal
public List<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null){
return list;
}
list.add(root.val);
list.addAll(preorderTraversal(root.left));
list.addAll(preorderTraversal(root.right));
return list;
}
Inorder traversal
public List<Integer> inorderTraversal(TreeNode root) {
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null){
return list;
}
list.addAll(inorderTraversal(root.left));
list.add(root.val);
list.addAll(inorderTraversal(root.right));
return list;
}
post-order traversal
public List<Integer> postorderTraversal(TreeNode root) {
ArrayList<Integer> list=new ArrayList<Integer>();
if(root==null){
return list;
}
list.addAll(postorderTraversal(root.left));
list.addAll(postorderTraversal(root.right));
list.add(root.val);
return list;
}
level-order traversal
public List<List<Integer>> levelOrder(TreeNode root) {
LinkedList<TreeNode> queue=new LinkedList<TreeNode>();
ArrayList<Integer> levelList=new ArrayList<Integer>();
ArrayList<List<Integer>> list=new ArrayList<List<Integer>>();
queue.offer(root);
while(!queue.isEmpty()){
int size=queue.size();
for(int i=0;i<size;i++){
TreeNode node=queue.poll();
if(node==null) continue;
levelList.add(node.val);
if(node.left!=null){
queue.offer(node.left);
}
if(node.right!=null){
queue.offer(node.right);
}
if(i==size-1){
list.add(levelList);
levelList=new ArrayList<Integer>();
}
}
}
return list;
}
recursion problem
tree depth
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}
int l=maxDepth(root.left);
int r=maxDepth(root.right);
return l>r?l+1:r+1;
}
mirror tree
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return st(root.left,root.right);
}
public boolean st(TreeNode lNode,TreeNode rNode){
if(lNode==null&&rNode==null){
return true;
}else if(lNode!=null&&rNode!=null){
return (lNode.val==rNode.val) && st(lNode.left,rNode.right) && st(lNode.right,rNode.left);
}else{
return false;
}
}
Is there a path to the leaf node (I think this translation is more appropriate)
public boolean hasPathSum(TreeNode root, int sum) {
if(root==null){
return false;
}
if(root.left==null&&root.right==null&&root.val==sum){
return true;
}
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
}
get binary tree from inorder postorder
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(postorder.length<1){
return null;
}
TreeNode root=new TreeNode(postorder[postorder.length-1]);
int position=0;
for(int i=0;i<inorder.length;i++){
if(root.val==inorder[i]){
position=i;
break;
}
}
int[] inorderL=new int[position];
int[] inorderR=new int[inorder.length-1-position];
int[] postorderL=new int[position];
int[] postorderR=new int[inorder.length-1-position];
System.arraycopy(inorder,0,inorderL,0,position);
System.arraycopy(inorder,position+1,inorderR,0,inorder.length-1-position);
System.arraycopy(postorder,0,postorderL,0,position);
System.arraycopy(postorder,position,postorderR,0,inorder.length-1-position);
root.left=buildTree(inorderL,postorderL);
root.right=buildTree(inorderR,postorderR);
return root;
}
get binary tree from preorder inorder
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length<1){
return null;
}
TreeNode root=new TreeNode(preorder[0]);
int position=0;
for(int i=0;i<inorder.length;i++){
if(root.val==inorder[i]){
position=i;
break;
}
}
int[] inorderL=new int[position];
int[] inorderR=new int[inorder.length-1-position];
int[] preorderL=new int[position];
int[] preorderR=new int[inorder.length-1-position];
System.arraycopy(inorder,0,inorderL,0,position);
System.arraycopy(inorder,position+1,inorderR,0,inorder.length-1-position);
System.arraycopy(preorder,1,preorderL,0,position);
System.arraycopy(preorder,position+1,preorderR,0,inorder.length-1-position);
root.left=buildTree(preorderL,inorderL);
root.right=buildTree(preorderR,inorderR);
return root;
}
nearest parent node
private TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) {
return null;
}
if (root.val == p.val) return p;
if (root.val == q.val) return q;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if (left != null && right != null) {
return root;
}
return left != null ? left : right;
}
other
Hierarchical link
private void connect(TreeLinkNode root) {
LinkedList<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeLinkNode node = queue.poll();
if (i == size - 1) {
node.next = null;
} else {
node.next = queue.peek();
}
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
}
Since the specific topics are all in LeetCode, only the solutions are posted here. specific code