二叉树
1、遍历
递归
ArrayList<Integer> preList = new ArrayList<>();
ArrayList<Integer> inList = new ArrayList<>();
ArrayList<Integer> postList = new ArrayList<>();
public int[][] threeOrders (TreeNode root) {
// write code here
preOrder(root);
inOrder(root);
postOrder(root);
int[][] res = new int[3][preList.size()];
res[0] = preList.stream().mapToInt(Integer::intValue).toArray();
res[1] = inList.stream().mapToInt(Integer::intValue).toArray();
res[2] = postList.stream().mapToInt(Integer::intValue).toArray();
return res;
}
private void preOrder(TreeNode root){
if(root == null) return;
preList.add(root.val);
preOrder(root.left);
preOrder(root.right);
}
private void inOrder(TreeNode root){
if(root == null) return ;
inOrder(root.left);
inList.add(root.val);
inOrder(root.right);
}
private void postOrder(TreeNode root){
if(root == null) return;
postOrder(root.left);
postOrder(root.right);
postList.add(root.val);
}
迭代
前序
private void preOrder1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
preList.add(node.val);
if(node.right != null) stack.push(node.right);
if(node.left != null) stack.push(node.left);
}
}
中序
private void inOrder1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
if(cur != null){
stack.push(cur);
cur = cur.left;//往左走,走到头
}else{
cur = stack.pop();//往中间走
inList.add(cur.val);
cur = cur.right;//往右走
}
}
}
后序
private void postOrder1(TreeNode root){
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.pop();
postList.add(node.val);
if(node.left != null) stack.push(node.left);
if(node.right != null) stack.push(node.right);
}
Collections.reverse(postList);
}
二叉搜索树中第K小的元素
中序遍历递归
int count = 0,int res;
public int kthSmallest(TreeNode root, int k) {
inOrder(root,k);
return res;
}
private void inOrder(TreeNode node,int k){
if(node == null) return;
inOrder(node.left,k);
count++;
if(count == k){
res = node.val;
return;
}
inOrder(node.right,k);
}
中序遍历迭代
public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
int count = 0,res = 0;
while(cur != null || !stack.isEmpty()){
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
count++;
if(count == k){
res = cur.val;
break;
}
cur = cur.right;
}
}
return res;
}
public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()){
if(cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
if(--k == 0) break;
cur = cur.right;
}
}
return cur.val;
3、分治法
public int kthSmallest(TreeNode root, int k) {
int n = count(root.left);
if(n + 1 < k){//表示需要结点在右子树中 n表示左子树的结点 1表示当前结点
return kthSmallest(root.right,k - n -1);
}else if(n + 1 > k){
return kthSmallest(root.left,k);
}else{
return root.val;
}
}
private int count(TreeNode node){//计算节点数
if(node == null) return 0;
return 1 + count(node.left) &#