zoukankan      html  css  js  c++  java
  • leetcode — binary-tree-inorder-traversal

    import java.util.Arrays;
    import java.util.Stack;
    import java.util.TreeMap;
    
    /**
     *
     * Source : https://oj.leetcode.com/problems/binary-tree-inorder-traversal/
     *
     *
     * Given a binary tree, return the inorder traversal of its nodes' values.
     *
     * For example:
     * Given binary tree {1,#,2,3},
     *
     *    1
     *     
     *      2
     *     /
     *    3
     *
     * return [1,3,2].
     *
     * Note: Recursive solution is trivial, could you do it iteratively?
     *
     * confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.
     *
     * OJ's Binary Tree Serialization:
     *
     * The serialization of a binary tree follows a level order traversal, where '#' signifies
     * a path terminator where no node exists below.
     *
     * Here's an example:
     *
     *    1
     *   / 
     *  2   3
     *     /
     *    4
     *     
     *      5
     *
     * The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".
     *
     */
    public class BinaryTreeInOrderTraversal {
        private int[] result = null;
        int pos = 0;
    
        public int[] traversal(char[] tree) {
            result = new int[tree.length];
            pos = 0;
            traversalByRecursion(createTree(tree));
            return result;
        }
    
        public int[] traversal1(char[] tree) {
            result = new int[tree.length];
            pos = 0;
            traversalbyIterator(createTree(tree));
            return result;
        }
    
        /**
         * 对二叉树进行中序遍历
         *
         * 树的遍历分为:
         *  深度优先:
         *      先序遍历:先访问根节点然后依次访问左右子的节点
         *      中序遍历:先访问左子节点,然后访问根节点,在访问右子节点
         *      后序遍历:先访问左右子节点,然后访问根节点
         *
         * 先用递归实现中序遍历
         *
         * @param root
         * @return
         */
        public void traversalByRecursion(TreeNode root) {
            if (root == null) {
                return;
            }
            traversalByRecursion(root.leftChild);
            result[pos++] = root.value;
            traversalByRecursion(root.rightChild);
        }
    
        /**
         * 使用循环来进行中序遍历,借助栈实现
         *
         * @param root
         */
        public void traversalbyIterator (TreeNode root) {
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode cur = root;
            while (cur != null || stack.size() > 0) {
                if (cur == null) {
                    // 当前节点为空,表示已经是叶子节点,
                    TreeNode node = stack.pop();
                    result[pos++] = node.value;
                    cur = node.rightChild;
                } else {
                    stack.push(cur);
                    cur = cur.leftChild;
                }
            }
        }
    
    
        public TreeNode createTree (char[] treeArr) {
            TreeNode[] tree = new TreeNode[treeArr.length];
            for (int i = 0; i < treeArr.length; i++) {
                if (treeArr[i] == '#') {
                    tree[i] = null;
                    continue;
                }
                tree[i] = new TreeNode(treeArr[i]-'0');
            }
            int pos = 0;
            for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
                if (tree[i] != null) {
                    tree[i].leftChild = tree[++pos];
                    if (pos < treeArr.length-1) {
                        tree[i].rightChild = tree[++pos];
                    }
                }
            }
            return tree[0];
        }
    
    
    
        private class TreeNode {
            TreeNode leftChild;
            TreeNode rightChild;
            int value;
    
            public TreeNode(int value) {
                this.value = value;
            }
    
            public TreeNode() {
            }
        }
    
        public static void main(String[] args) {
            BinaryTreeInOrderTraversal binaryTreeInOrderTraversal = new BinaryTreeInOrderTraversal();
            char[] arr1 = new char[]{'1','#','2','3'};
            char[] arr2 = new char[]{'1','2','3','#','#','4','#','#','5'};
            System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal(arr1)));
            System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal(arr2)));
    
            System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal1(arr1)));
            System.out.println(Arrays.toString(binaryTreeInOrderTraversal.traversal1(arr2)));
        }
    }
    
  • 相关阅读:
    函数 free 的原型
    malloc 返回值的类型是 void *
    malloc 函数本身并不识别要申请的内存是什么类型
    用 free 或 delete 释放了内存之后,立即将指针设置为 NULL,防止产 生“野指针”
    动态内存的申请与释放必须配对,防止内存泄漏
    避免数组或指针的下标越界,特别要当心发生“多 1”或者“少 1” 操作
    不要忘记为数组和动态内存赋初值
    用 malloc 或 new 申请内存之后,应该立即检查指针值是否为 NULL
    释放了内存却继续使用它
    忘记了释放内存,造成内存泄露
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7788353.html
Copyright © 2011-2022 走看看