1. 程式人生 > >【LeetCode】Binary Tree Preorder Traversal 二叉樹前序遍歷遞迴以及非遞迴演算法

【LeetCode】Binary Tree Preorder Traversal 二叉樹前序遍歷遞迴以及非遞迴演算法

 
Total Accepted: 17403 Total Submissions: 50093 My Submissions
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
   1
    \
     2
    /
   3
return [1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?
【解題思路】
1、遞迴

Java AC

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private ArrayList<Integer> list;
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        list = new ArrayList<Integer>();
        preOrder(root);
        return list;
    }
    private void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        list.add(root.val);
        preOrder(root.left);
        preOrder(root.right);
    }
}
2、非遞迴 棧作為輔助,每次先放入右孩子,再放入左孩子。依次出棧就是前序遍歷。

Java AC

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    private ArrayList<Integer> list;
    public ArrayList<Integer> preorderTraversal(TreeNode root) {
        list = new ArrayList<Integer>();
        preOrder(root);
        return list;
    }
    private void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode node = stack.pop();
            list.add(node.val);
            if(node.right != null){
                stack.push(node.right);
            }
            if(node.left != null){
                stack.push(node.left);
            }
        }
    }
}


相關推薦

LeetCodeBinary Tree Preorder Traversal 以及演算法

  Total Accepted: 17403 Total Submissions: 50093 My Submissions Given a binary tree, return the preorder traversal of its nodes' values.

LeetCodeBinary Tree Inorder Traversal 以及演算法

  Total Accepted: 16494 Total Submissions: 47494 Given a binary tree, return the inorder traversal of its nodes' values. For example: Giv

LeetCodeBinary Tree Postorder Traversal 以及演算法

Total Accepted: 15614 Total Submissions: 50928 My Submissions Given a binary tree, return the postorder traversal of its nodes' values.

LeetCode144. Binary Tree Preorder Traversal 實現

題目: 翻譯:給定一個二叉樹,返回先序遍歷的序列。 分析:二叉樹的先序遍歷、中序遍歷及後序遍歷演算法是資料結構中最基礎的遍歷演算法。             先序遍歷:先訪問根節點的資料,再訪問左孩子節點的資料,最後訪問右孩子節點的資料。圖中例子先序遍歷輸出的序列為:【

binary-tree-inorder-traversal——

str () init inorder code while urn value public Given a binary tree, return the inordertraversal of its nodes‘ values. For example:Given

Leetcode 94. Binary Tree Inorder Traversalt ()

原題 Given a binary tree, return the inorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output

LeetCode 144. Binary Tree Preorder Traversal C++

do it emp 二叉 c++ pre value code val right Given a binary tree, return the preorder traversal of its nodes‘ values. Example: Input: [1

[Leetcode 144]Binary Tree Preorder Traversal

public list left 前序 output nod roo strong while 【題目】 Given a binary tree, return the preordertraversal of its nodes‘ values. Example: Inp

[LeetCode] Binary Tree Preorder Traversal 的先

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3

LeetCode 144 Binary Tree Preorder Traversal

Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr

LeetCode 94.Binary Tree Inorder Traversal (的中)

題目描述: 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? AC C++ Solution

[LeetCode] Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9

LeetCode 94. Binary Tree Inorder Traversal 的中 C++

壓入 rsa return recursive 使用 rdquo push cto 方法 Given a binary tree, return the inorder traversal of its nodes‘ values. Example: Input:

數據結構35:、中和後

tdi 代碼 nod 完成 循環 同時 reat pan 設置 遞歸算法底層的實現使用的是棧存儲結構,所以可以直接使用棧寫出相應的非遞歸算法。 先序遍歷的非遞歸算法 從樹的根結點出發,遍歷左孩子的同時,先將每個結點的右孩子壓棧。當遇到結點沒有左孩子的時候,取棧頂的右

leetcode144 與中的不同

二叉樹中序遍歷 class Solution { public: vector<int> inorderTraversal(TreeNode* root) { TreeNode* cur=root; vector<int>

根據和中序列求解後演算法

問題模型:已知某二叉樹前序遍歷序列為1,2,3,4,5,6,中序遍歷為3,2,4,1,6,5,設計程式計算後序序列。 關於這個問題你可以通過前序遍歷和中序遍歷建立一顆樹,然後通過後序遍歷進行求解,當然還可以直接根據已知兩序列直接推理後序序列,本文將對這種分析進行介紹。 利用

資料結構與演算法 (十)

名詞解釋  度數(degree) 一個結點的子樹個數  樹葉(leaf) 沒有子樹的結點稱為樹葉或終端結點  分支結點(branch node) 非終端結點  子女(child)和兒子(son)非終端結點  父母(parent)若

演算法

       前幾天參加了阿里暑期實習的內推面試,發現自己的資料結構演算法基礎特別薄弱,比如其中一個問題是中序遍歷的遞迴與非遞迴演算法,我平時看資料結構只知道遞迴演算法,非遞迴的演算法直接被問懵逼了,在思考了幾十秒之後想出了用陣列存放每次遍歷節點的父節點,然後用for迴圈遍

、中、後、層的直觀理解

0. 寫在最前面     複習到二叉樹,看到網上諸多部落格文章各種繞,記得頭暈。個人覺得數學、演算法這些東西都是可以更直觀簡潔地表示,然後被記住的,並不需要靠死記硬背。 本文的程式基本來源於《大話資料結構》,個人感覺是一本非常好的書,推薦去看。 1. 為什麼叫前序、

typedef struct { BTNode* left; BTNode* right; int data; } BTNode; typedef void (*visit_t)(BTNode* node); void traversal(BTN