Brush on the buckle these days
Some binary tree problems, rely on the basis (pre order traversal ~ middle order traversal ~ subsequent traversal ~ sequence traversal) to solve the problem. Of course, there are many methods to solve the problem.
[introduce the basic traversal first]
Preorder traversal
The first step is to establish a loop, because the preorder traversal (following the left and right) first puts the root node into the list and looks to the left until the last node on the left. Therefore, all nodes along the left are put into the list.
Step 2: at this time, the top element of the stack is the last node on the left side of the tree to find the right tree.
class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> mylist = new ArrayList<Integer>(); Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root;Record root node while( cur != null || !stack.isEmpty()) { while (cur != null) { stack.push(cur);Put the root node on the stack mylist.add(cur.val);Because it's the first order(Left and right) put the value of the heel into list in cur = cur.left;Go to the left until there is no element on the left. At this time, the left has reached the last node } TreeNode pre = stack.pop();Push the top element out of the stack cur = pre.right;Develop to the right } return mylist;Last return list; }
Middle order traversal
The first step is to establish a loop, because the middle order traversal (left and right) first puts the root node into the stack and looks for the left node until the last node on the left. At this time, put the left node into the list in turn
Step 2: at this time, the top element of the stack is the last node on the left side of the tree to find the right tree.
class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root;Record and node while(cur != null || !stack.empty()){ while(cur != null){ stack.push(cur );Put the root node on the stack cur = cur.left;Go to the left until the left is empty } TreeNode pre = stack.pop(); list.add( pre.val);Put the root node into list in cur = pre.right; } return list; } }
Sequence traversal
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ret = new LinkedList<>(); Queue<TreeNode> myqueue = new LinkedList<>(); if(root == null) return ret; myqueue.offer(root); while(!myqueue.isEmpty()){ List<Integer> list = new LinkedList<>(); int size = myqueue.size(); while(size != 0){ TreeNode cur = myqueue.poll(); list.add(cur.val); if(cur.left != null){ myqueue.offer(cur.left); } if(cur.right != null){ myqueue.offer(cur.right); } size--; } ret.add(list); } return ret; } }
Binary tree to linked list (force buckle 114)
The first step is to use pre order traversal to put the node values into the linked list
The second step is to calculate the length of the linked list and use the loop to transform it into the form of the linked list
class Solution { public void flatten(TreeNode root) { List<TreeNode> list = new ArrayList<>(); dfs(root,list); int size = list.size();Calculate the length of the linked list for(int i = 1; i < size; i ++){Use cycle TreeNode pre = list.get(i-1),cur = list.get(i);Define the first element as pre Second element definition cur pre.left = null;Left blank pre.right = cur; pre The right side of the is cur } } void dfs(TreeNode root, List<TreeNode> list ){ if(root != null){ Here we use preorder traversal list.add(root); dfs(root.left,list); dfs(root.right,list); } } }
Next node of binary tree
Step 1: because pNode is not the root node, use next to find the following node
The second step {uses the middle order traversal to store the nodes in the list
The third step is to traverse the entire linked list and use the loop to find the next node value of pNode
import java.util.*; public class Solution { LinkedList<TreeLinkNode> list = new LinkedList<>(); public TreeLinkNode GetNext(TreeLinkNode pNode) { TreeLinkNode head = pNode; while(head.next != null){ head = head.next; } search(head); for(int i = 0 ; i < list.size(); i ++){ if(pNode == list.get(i)){ return i == list.size() -1? null: list.get(i+1); } } return null; } public void search(TreeLinkNode pnode){ if(pnode == null) return ; search( pnode.left); list.add(pnode); search( pnode.right); } }
A path with a value in a binary tree
The foundation is based on the recursive method. First, put the root node into lists and subtract the current node value from the value to be found
Recursive left tree and right tree until the target value is 0; And only when the left subtree and the right subtree are recursively completed can it be called completely finding a branch.
class Solution { List<List<Integer>> ret = new LinkedList<>(); LinkedList<Integer> lists = new LinkedList<>(); public List<List<Integer>> pathSum(TreeNode root, int target) { dfs(root,target); return ret; } public void dfs(TreeNode root,int targets){ if(root == null) return ; lists.add(root.val); targets -=root.val; dfs( root.left, targets ); dfs( root.right, targets ); if(root.left == null && root.right == null && targets == 0){ ret.add(new LinkedList(lists)); } lists.removeLast() ; } }
Print binary tree into multiple lines
First of all, it is established in sequence traversal. First, put the root node into the queue and find its left node or right node for traversal
After traversing each layer, it will be put together in the form of an array.
public class Solution { ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) { ArrayList<ArrayList<Integer> > ret = new ArrayList<>(); Queue<TreeNode> myqueue = new LinkedList<>(); if(pRoot == null) return ret; myqueue.offer(pRoot);Put the root node into it while(!myqueue.isEmpty()){ int size = myqueue.size();Calculate queue length ArrayList<Integer> list = new ArrayList<>(); while(size != 0){ TreeNode cur = myqueue.poll(); list.add(cur.val );Put the node value into the linked list if(cur.left != null){When the left side is not empty myqueue.offer(cur.left);Put in queue } if(cur.right != null){When the right side is not empty myqueue.offer(cur.right);Put in queue } size--; } ret.add(list); } return ret;Last return } }