美文网首页
二叉树操作

二叉树操作

作者: laowangv2 | 来源:发表于2019-03-03 22:43 被阅读0次

遍历

代码形式采用leetcode Solution

中序

变形:第k小元素

class Solution {
    //递归
    private void inOrder(List<Integer> list, TreeNode root) {
        if(root == null)
            return;
        inOrder(list, root.left);
        list.add(root.val);
        inOrder(list, root.right);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList();
        // inOrder(ret, root);
        //非递归
        Stack<TreeNode> stack = new Stack();
        TreeNode node = root;
        while(node != null || !stack.isEmpty()) {
            if(node == null) {
                node = stack.pop();
                ret.add(node.val);
                node = node.right;
                continue;
            }
            stack.push(node);
            node = node.left;
        }
        return ret;
    }
}

前序

class Solution {
    //递归
    public void preOrder(List<Integer> list, TreeNode root) {
        if(root==null)
            return;
        list.add(root.val);
        preOrder(list, root.left);
        preOrder(list, root.right);
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList();
        // preOrder(ret, root);
        //非递归
        Stack<TreeNode> s = new Stack();
        TreeNode node = root;
        while(node != null || !s.isEmpty()) {
            if(node == null) {
                node = s.pop();
                node = node.right;
                continue;
            }
            ret.add(node.val);
            s.push(node);
            node=node.left;
        }
        return ret;
    }
}

后序

class Solution {
    //递归
    public void postOrder(List<Integer> list, TreeNode root) {
        if(root == null)
            return;
        postOrder(list, root.left);
        postOrder(list, root.right);
        list.add(root.val); 
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList();
        // postOrder(ret, root);
        //非递归
        Stack<TreeNode> s = new Stack();
        TreeNode node = root;
        Map<TreeNode, Boolean> traversaled = new HashMap();
        while (node != null || !s.isEmpty()) {
            if(node == null) {
                node = s.pop();
                Boolean state = traversaled.get(node);
                if (state==null || state==false) {
                    s.push(node);
                    traversaled.put(node, true);
                    node = node.right;
                    continue;
                }
                ret.add(node.val);
                //这里比较骚,不能pop,否则死循环
                node = null;
                continue;
            }
            s.push(node);
            node = node.left;
        }
        return ret;
    }
}

高度

class Solution {
    //递归
    // public int maxDepth(TreeNode root) {
    //     if(root == null)
    //         return 0;
    //     return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    // }
    //非递归
    public int maxDepth(TreeNode root) {
        if(root == null)
            return 0;
        int depth = 0;
        TreeNode node = root;
        Queue<TreeNode> queue = new LinkedList();
        int cnt = 1;
        int next = 1;
        queue.offer(node);
        while(!queue.isEmpty()) {
            depth++;
            cnt = next;
            next = 0;
            while(cnt > 0) {
                node = queue.poll();
                cnt--;
                if (node.left != null){
                    queue.offer(node.left);
                    next++;
                }
                if (node.right != null) {
                    queue.offer(node.right);
                    next++;
                }
            }
        }
        return depth;
    }
}

判断是否平衡

class Solution {
    //照搬c++思路,賊几把不优雅  
    private static class Depth {
        int val;
    }
    
    private boolean getDepth(TreeNode root, Depth depth) {
        if(root == null) {
            depth.val=0;
            return true;
        }
        Depth left = new Depth();
        Depth right = new Depth();
        boolean l = getDepth(root.left, left);
        boolean r = getDepth(root.right, right);
        depth.val = 1 + Math.max(left.val, right.val);
        if (l && r) {
            return Math.abs(left.val-right.val) <= 1;
        }
        return false;
    }
    public boolean isBalanced(TreeNode root) {
        Depth depth = new Depth();
        return getDepth(root, depth);
    }
}

相关文章

  • 68_二叉树的比较与相加

    关键词:二叉树的克隆操作、二叉树比较操作、二叉树的相加操作 0. 二叉树的克隆操作 SharedPointer< ...

  • 二叉树链式存储

    一、二叉树构造 二、二叉树基本操作 1、打印数据 2、构造空二叉树T 销毁二叉树初始条件: 二叉树T存在。操作结果...

  • 平衡二叉树的基本操作

    平衡二叉树定义及操作原理 C++简单实现 涉及练习题目:平衡二叉树的基本操作

  • 二叉树与图

    二叉树深度搜索 1. 路径总和 II 前序操作和后序操作结合: 2.二叉树的最近公共祖先 3. 二叉树展开为链表...

  • 二叉树的镜像

    操作给定的二叉树,将其变换为源二叉树的镜像。

  • 二叉树的镜像

    操作给定的二叉树,将其变换为源二叉树的镜像。

  • Day18 剑指offer:二叉树镜像

    操作给定的二叉树,将其变换为源二叉树的镜像。

  • 二叉树的镜像

    操作给定的二叉树,将其变换为源二叉树的镜像。

  • 二叉树的镜像

    题目: 操作给定的二叉树,将其变换为源二叉树的镜像。

  • 二叉树

    一些关于二叉树的简单操作 创建节点 简单操作

网友评论

      本文标题:二叉树操作

      本文链接:https://www.haomeiwen.com/subject/waimuqtx.html