国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

Binary Tree Traversal

浠ラ箍 / 2015人閱讀

PostOrder

public class Solution {
    // Important, when you pop a node, ensure its children are traversed.
    public List postorderTraversal(TreeNode root) {
        ArrayDeque s = new ArrayDeque();
        List ans = new ArrayList();
        TreeNode cur = root;
        
        while (cur != null || !s.isEmpty()) {
            while (cur != null) {
                s.push(cur);
                cur = cur.left;
            }
            
            while (!s.isEmpty() && cur == s.peek().right) {
                cur = s.pop();
                ans.add(cur.val);
            }
            
            if (s.isEmpty()) cur = null; else cur = s.peek().right;
        }
        
        return ans;
    }
    
}

PreOrder

public class Solution {
    public List preorderTraversal(TreeNode root) {
        List res = new LinkedList();
        ArrayDeque stk = new ArrayDeque();
        TreeNode cur = root;
        
        while(cur != null || !stk.isEmpty()){
            if(cur != null) {
                stk.push(cur);
                res.add(cur.val);   // add val before going to children
                cur = cur.left;
            } else {
                TreeNode node = stk.pop();
                cur = node.right;
            }
        }
        return res;
    }
}

InOrder

public class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList();
        if(root == null) return res;
        ArrayDeque stk = new ArrayDeque();
        TreeNode cur = root;
        
        while(cur != null || !stk.isEmpty()){
            if(cur !=null) {
                stk.push(cur);
                cur = cur.left; 
            } else {
                TreeNode node = stk.pop();
                res.add(node.val);        // add after all left children
                node = node.right;
            }
        }
        return res;
    }
}

PreOrder

public class Solution {
    public List preorderTraversal(TreeNode root) {
        List res = new LinkedList();
        ArrayDeque stk = new ArrayDeque();
        if(root == null) return res;
        TreeNode cur = root;
        stk.push(cur);
        
        while(!stk.isEmpty()){
            cur = stk.pop();
            res.add(cur.val);
            if(cur.right != null) stk.push(cur.right);
            if(cur.left != null) stk.push(cur.left);
        }
        return res;
    }
}

InOrder

public class Solution {
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList();
        if(root == null) return res;
        ArrayDeque stk = new ArrayDeque();
        TreeNode cur = root;
        
        while(cur != null || !stk.isEmpty()){
            while(cur != null) {
                stk.push(cur);
                cur = cur.left;
            }
            cur = stk.pop();
            res.add(cur.val);
            cur = cur.right;
        }
        return res;
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/66538.html

相關(guān)文章

  • [Leetcode] Binary Tree Traversal 二叉樹(shù)遍歷

    摘要:棧迭代復(fù)雜度時(shí)間空間遞歸棧空間對(duì)于二叉樹(shù)思路用迭代法做深度優(yōu)先搜索的技巧就是使用一個(gè)顯式聲明的存儲(chǔ)遍歷到節(jié)點(diǎn),替代遞歸中的進(jìn)程棧,實(shí)際上空間復(fù)雜度還是一樣的。對(duì)于先序遍歷,我們出棧頂節(jié)點(diǎn),記錄它的值,然后將它的左右子節(jié)點(diǎn)入棧,以此類推。 Binary Tree Preorder Traversal Given a binary tree, return the preorder tr...

    RaoMeng 評(píng)論0 收藏0
  • [Leetcode-Tree]Binary Tree Level Order Traversal

    摘要:解題思路層次遍歷二叉樹(shù),我們采用隊(duì)列,本題的注意點(diǎn)是需要分割出每一層的序列,所以在從隊(duì)列中取元素之前,我們要先記錄隊(duì)列的大小,以表示這一層中節(jié)點(diǎn)的個(gè)數(shù)。 Binary Tree Level Order TraversalGiven a binary tree, return the level order traversal of its nodes values. (ie, from...

    Half 評(píng)論0 收藏0
  • [Leetcode] Construct Binary Tree from Traversal 根據(jù)

    摘要:二分法復(fù)雜度時(shí)間空間思路我們先考察先序遍歷序列和中序遍歷序列的特點(diǎn)。對(duì)于中序遍歷序列,根在中間部分,從根的地方分割,前半部分是根的左子樹(shù),后半部分是根的右子樹(shù)。 Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, constru...

    caoym 評(píng)論0 收藏0
  • leetcode102. Binary Tree Level Order Traversal

    摘要:題目要求對(duì)于一棵樹(shù)進(jìn)行序遍歷。水平遍歷即遍歷結(jié)束當(dāng)前行以后再遍歷下一行,并將每行的結(jié)果按行填入到數(shù)組中返回。利用水平遍歷的話,我們只需要知道當(dāng)前元素在樹(shù)中的高度就可以知道應(yīng)當(dāng)插入到那個(gè)數(shù)組中。 題目要求 Given a binary tree, return the level order traversal of its nodes values. (ie, from left to...

    Coding01 評(píng)論0 收藏0
  • leetcode講解--94. Binary Tree Inorder Traversal

    摘要:題目地址嗯,經(jīng)典的題目,中序遍歷二叉樹(shù)。代碼如下中序遍歷先序遍歷后序遍歷是不是簡(jiǎn)單的不要不要的,遞歸就是這么美。右孩子后壓棧全部釋放出來(lái)嗯,總的來(lái)說(shuō)用迭代遍歷確實(shí)燒腦,沒(méi)什么性能上的特殊需求還是用遞歸寫法吧,對(duì)程序員友好哦。 94. Binary Tree Inorder Traversal Given a binary tree, return the inorder travers...

    henry14 評(píng)論0 收藏0
  • [LeetCode] 429. N-ary Tree Level Order Traversal (

    429. N-ary Tree Level Order Traversal Given an n-ary tree, return the level order traversal of its nodes values. (ie, from left to right, level by level). For example, given a 3-ary tree:showImg(https...

    LiangJ 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

浠ラ箍

|高級(jí)講師

TA的文章

閱讀更多
最新活動(dòng)
閱讀需要支付1元查看
<