LeetCode094 Binary Tree Inorder Traversal

来源:互联网 发布:大数据来源 编辑:程序博客网 时间:2024/05/29 18:24

详细见:leetcode.com/problems/binary-tree-inorder-traversal


Java Solution: github

package leetcode;import java.util.LinkedList;import java.util.List;import java.util.Queue;import java.util.Stack;import tools.TreeNode辅助.TreeNode;;/* * Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree [1,null,2,3],   1    \     2    /   3return [1,3,2]. */public class P094_BinaryTreeInorderTraversal {public static void main(String[] args) {TreeNode t0 = new TreeNode(0);TreeNode t1 = new TreeNode(1);TreeNode t2 = new TreeNode(2);TreeNode t3 = new TreeNode(3);TreeNode t4 = new TreeNode(4);TreeNode t5 = new TreeNode(5);TreeNode t6 = new TreeNode(6);t0.left = t1;t0.right = t2;t1.left = t3;t1.right = t4;t2.left = t5;t2.right = t6;List<Integer> ans = new Solution6().inorderTraversal(t0);tools.Utils.B_打印List_Integer(ans);}/* * AC * 0 ms * 这个时候递归版本 */static class Solution1 {List<Integer> ans = new LinkedList<Integer>();    public List<Integer> inorderTraversal(TreeNode root) {    in(root);        return ans;    }    void in(TreeNode root) {    if (root == null) {    return;    }    in(root.left);    ans.add(root.val);    in(root.right);    }}/* * 写了一个层序遍历 */static class Solution2 {List<Integer> ans = new LinkedList<Integer>();    Queue<TreeNode> queue = new LinkedList<TreeNode>();    public List<Integer> inorderTraversal(TreeNode root) {    if (root == null) {    return ans;    }    queue.add(root);    while (! queue.isEmpty()) {    TreeNode root_now = queue.poll();    ans.add(root_now.val);    if (root_now.left != null) {    queue.add(root_now.left);    }    if (root_now.right != null) {    queue.add(root_now.right);    }    }        return ans;    }}/* * 写了一个先序遍历 */static class Solution3 {List<Integer> ans = new LinkedList<>();public List<Integer> inorderTraversal(TreeNode root) {if (root == null) {return ans;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (! stack.isEmpty()) {TreeNode root_now = stack.pop();ans.add(root_now.val);if (root_now.right != null) {stack.push(root_now.right);}if (root_now.left != null) {stack.push(root_now.left);}}return ans;}}/* * 写了一个后序遍历 */static class Solution4 {List<Integer> ans = new LinkedList<>();public List<Integer> inorderTraversal(TreeNode root) {if (root == null) {return ans;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (! stack.isEmpty()) {TreeNode root_now = stack.peek();if (null != root_now.left && root != root_now.left && root != root_now.right) {stack.push(root_now.left);} else if (null != root_now.right && root != root_now.right) {stack.push(root_now.right);} else {ans.add(stack.pop().val);root = root_now;}}return ans;}}/* * 写了一个后序遍历 */static class Solution5 {List<Integer> ans = new LinkedList<>();public List<Integer> inorderTraversal(TreeNode root) {if (null == root) {return ans;}Stack<TreeNode> stack1 = new Stack<>();Stack<TreeNode> stack2 = new Stack<>();stack1.push(root);while (! stack1.isEmpty()) {TreeNode root_now = stack1.pop();stack2.push(root_now);if (null != root_now.left) {stack1.push(root_now.left);}if (null != root_now.right) {stack1.push(root_now.right);}}while (! stack2.isEmpty()) {ans.add(stack2.pop().val);}return ans;}}/* * 写了一个中序遍历 */static class Solution6 {List<Integer> ans = new LinkedList<Integer>();public List<Integer> inorderTraversal(TreeNode root) {if (null == root) {return ans;}Stack<TreeNode> stack = new Stack<TreeNode>();while (! stack.isEmpty() || root != null) {if (null != root) {stack.push(root);root = root.left;} else {root = stack.pop();ans.add(root.val);root = root.right;}}return ans;}}}


C Solution: github

/*    url: leetcode.com/problems/binary-tree-inorder-traversal    in_order:          AC 3ms 0.00%    in_order_unrecur:  AC 0ms 62.86%*/#include <stdio.h>#include <stdlib.h>typedef struct TreeNode stn;typedef struct TreeNode * ptn;typedef int T;typedef struct al sal;typedef struct al * pal;struct al {    int capacity;    int size;    T* arr;};pal al_init(int capacity) {    pal l = (pal) malloc(sizeof(sal));    if (capacity < 1) return NULL;    l->arr = (T*) malloc(sizeof(T) * capacity);    l->capacity = capacity;    l->size = 0;    return l;}void al_expand_capacity(pal l) {    T* new_arr = (T*) malloc(sizeof(T) * (l->capacity * 2 + 1));    int i = 0;    for (i = 0; i < l->capacity; i ++)        new_arr[i] = l->arr[i];    free(l->arr);    l->arr = new_arr;    l->capacity = l->capacity * 2 + 1;}void al_add_last(pal l, T v) {    if (l->capacity == l->size) al_expand_capacity(l);    l->arr[l->size] = v;    l->size ++;}T* al_convert_to_array_free_l(pal l) {    T* arr = l->arr;    free(l);    return arr;}struct TreeNode {    int val;    ptn left;    ptn right;};void in_order(ptn root, pal l) {    if (root == NULL) return;    in_order(root->left, l);    al_add_last(l, root->val);    in_order(root->right, l);}//element typetypedef ptn V;typedef struct dll sdll;typedef struct dll * pdll;typedef struct dln sdln;typedef struct dln * pdln;//doubly list nodestruct dln {    V val;    pdln pre;    pdln nxt;};//doubly linked liststruct dll {    pdln first;    pdln last;    int size;};pdll dll_init() {    pdll l = (pdll) malloc(sizeof(sdll));    l->first = NULL;    l->last = NULL;    l->size = 0;    return l;}void dll_add_last(pdll l, V v) {    pdln t = (pdln) malloc(sizeof(sdln));    t->val = v;    t->pre = NULL;    t->nxt = NULL;    if (l->size == 0) {        l->first = t;        l->last = t;        l->size = 1;        return;    }    t->pre = l->last;    l->last->nxt = t;    l->last = t;    l->size ++;}void dll_remove_last(pdll l) {    pdln t = NULL;    if (l->last == NULL) return;    if (l->first == l->last) {        free(l->first);        l->first = NULL;        l->last = NULL;        l->size = 0;        return;    }    t = l->last->pre;    t->nxt = NULL;    free(l->last);    l->last = t;    l->size --;}void dll_free_all(pdll l) {    pdln t1 = l->first, t2 = NULL;    while (t1 != NULL) {        t2 = t1->nxt;        free(t1);        t1 = t2;    }    free(l);}void in_order_unrecur(ptn root, pal l) {    pdll q = dll_init();    ptn now = root;    while (1) {        if (q->size == 0 && now == NULL) break;        if (now == NULL) {            now = q->last->val;            dll_remove_last(q);            al_add_last(l, now->val);            now = now->right;        } else {            dll_add_last(q, now);            now = now->left;        }    }    dll_free_all(q);}int* inorderTraversal(ptn root, int* rn) {    pal l = al_init(16);    //in_order(root, l);    in_order_unrecur(root, l);    *rn = l->size;    return al_convert_to_array_free_l(l);}


Python Solution: github

#coding=utf-8'''    url: leetcode.com/problems/binary-tree-inorder-traversal    @author:     zxwtry    @email:      zxwtry@qq.com    @date:       2017年4月24日    @details:    Solution: 42ms 63.63%    @details:    SolutionUnRecur: 52ms 24.22%'''class TreeNode(object):    def __init__(self, x):        self.val = x        self.left = None        self.right = Noneclass Solution(object):    def inOrder(self, root, a):        if (root == None): return        self.inOrder(root.left, a)        a.append(root.val)        self.inOrder(root.right, a)    def inorderTraversal(self, root):        """        :type root: TreeNode        :rtype: List[int]        """        a = []        self.inOrder(root, a)        return a    class SolutionUnRecur(object):    def inorderTraversal(self, root):        """        :type root: TreeNode        :rtype: List[int]        """        a, s, h = [], [], root        while (True):            if len(s) == 0 and h == None: break            if h == None:                h = s.pop()                a.append(h.val)                h = h.right            else:                s.append(h)                h = h.left                return a


0 0
原创粉丝点击