算法

·  阅读 104

获得二叉树深度的两种写法

1.每次传入深度,下一层就深度加一。

     int Height(TreeNode root,int depth){
        if(root==null) return depth;
        return Math.max(Height(root.left,depth+1),Height(root.right,depth+1));
    }
复制代码

2.从叶子就1+,回到上层就是深度。

    int Height(TreeNode root){
        if(root==null) return 0;
        return 1+Math.max(Height(root.left),Height(root.right));
    }
复制代码

来源:blog.csdn.net/Yonggie/art…

层次遍历

//非递归实现
    public ArrayList<Integer> printFromTopToBottom(BinaryTreeNode pTreeRoot){ //非递归版本
	ArrayList<Integer> list = new ArrayList<Integer>();
	if(pTreeRoot == null){
		return null;
	}
	Queue<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>(); 
	queue.offer(pTreeRoot);
	while(!queue.isEmpty()){
		BinaryTreeNode treeNode = queue.poll();
		if(treeNode.left!=null){
			queue.offer(treeNode.left);
		}
		if(treeNode.right!=null){
			queue.offer(treeNode.right);
		}
		list.add(treeNode.data);
	}
	return list;
}
复制代码
//二叉树递归实现
private int depth(BinaryTreeNode pTreeRoot){
	if(pTreeRoot==null){
		return 0;
	}
	int l = depth(pTreeRoot.left);
	int r = depth(pTreeRoot.right);
	if(l > r){
		return l + 1;
	}else{
		return r + 1;
	}
}
	
private void levelOrder(BinaryTreeNode pTreeNode, int level) {
	if(pTreeNode == null || level < 1){
		return ;
	}
	if(level == 1){
		System.out.print(pTreeNode.data+ " ");
		return ;
	}
	//左子树
	levelOrder(pTreeNode.left, level-1);
	//右子树
	levelOrder(pTreeNode.right, level-1);
}
	
public void printFromTopToBottom(BinaryTreeNode pTreeRoot){
	if(pTreeRoot==null){
		return ;
	}
	int depth = depth(pTreeRoot);
	for (int i = 1; i <= depth; ++i) {
		levelOrder(pTreeRoot, i);
	}
}
复制代码

来源:blog.csdn.net/u013132035/…

二叉树前序遍历

public class PreorderTraversal {
    List<Integer> res = new ArrayList<>();
    public static void main(String[] args) {

    }
    //迭代
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null){
            return res;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        TreeNode node;
        while (!stack.isEmpty()){
            node = stack.pop();
            if (node.right != null){
                stack.push(node.right);
            }
            if (node.left != null){
                stack.push(node.left);
            }
            res.add(node.val);
        }

        return res;
    }
    //递归
    public void recurse(TreeNode root) {
        if (root != null){
            res.add(root.val);
            recurse(root.left);
            recurse(root.right);
        }
    }
}
复制代码

二叉树中序遍历

public class InorderTraversal94 {
    List<Integer> res = new ArrayList<>();
    public static void main(String[] args) {

    }
    
    //迭代
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (root == null){
            return res;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode node = root;
        while(!stack.isEmpty() || node != null){
            while (node != null){
                stack.push(node);
                node = node.left;
            }
            node = stack.pop();
            res.add(node.val);
            node = node.right;
        }
        return res;
    }
    
    //递归
    public void recurse(TreeNode root) {
        if (root != null){
            recurse(root.left);
            res.add(root.val);
            recurse(root.right);
        }
    }
}
复制代码

二叉树后序遍历

public class PostorderTraversal {
    List<Integer> res = new ArrayList<>();
    public static void main(String[] args) {

    }
    //迭代
    public static List<Integer> postorderTraversal (TreeNode root){
        LinkedList<Integer> res = new LinkedList<>();
        if (root == null){
            return res;
        }
        LinkedList<TreeNode> stack = new LinkedList<>();
        stack.addLast(root);
        TreeNode node;
        while (!stack.isEmpty()){
            node = stack.pollLast();
            if (node.left != null){
                stack.addLast(node.left);
            }
            if (node.right != null){
                stack.addLast(node.right);
            }
            res.addFirst(node.val);
        }
        return res;
    }
    //递归
    public void recurse(TreeNode root) {
        if (root != null){
            res.add(root.val);
            recurse(root.left);
            recurse(root.right);
        }
    }
}
复制代码

经典算法——数组的循环(左移/右移)K位———思想

对于数组{1,2,3,4,5},k=2。

左移

1.反转前k部分:;{2,1,3,4,5};

2.反转后面部分:{2,1,5,4,3};

3.整体反转:{3,4,5,1,2};

右移

1.反转前n-k个部分:{3,2,1,4,5};

2.反转后面部分:{3,2,1,5,4};

3.整体反转:{4,5,1,2,3};

分类:
阅读
标签:
收藏成功!
已添加到「」, 点击更改