leetcode--Binary Tree Preorder Traversal

来源:互联网 发布:建模软件对比分析 编辑:程序博客网 时间:2024/05/16 00:25

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].

分类:二叉树

题意:前序遍历二叉树


解法1:递归

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<Integer> preorderTraversal(TreeNode root) {  
  12.         List<Integer> res = new ArrayList<Integer>();  
  13.         if(root!=null) helper(res,root);  
  14.         return res;  
  15.     }  
  16.       
  17.     public void helper(List<Integer> res,TreeNode n){  
  18.         res.add(n.val);  
  19.         if(n.left!=null) helper(res, n.left);  
  20.         if(n.right!=null) helper(res, n.right);  
  21.     }  
  22. }  


解法2:使用栈模拟递归

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<Integer> preorderTraversal(TreeNode root) {  
  12.         List<Integer> res = new ArrayList<Integer>();  
  13.         if(root==nullreturn res;  
  14.         Stack<TreeNode> stack = new Stack<TreeNode>();  
  15.         stack.add(root);  
  16.         while(!stack.isEmpty()){  
  17.             TreeNode cur = stack.pop();  
  18.             res.add(cur.val);  
  19.             if(cur.right!=null) stack.add(cur.right);//右节点先进栈,从而后出栈  
  20.             if(cur.left!=null) stack.add(cur.left);//左节点先进栈,从而先出栈  
  21.         }  
  22.         return res;  
  23.     }  
  24. }  

解法3:morris二叉树遍历,空间O(1),时间O(n)

遍历过程如下:对于某个节点root,判断其是否有左子树,没有,则访问该节点,将当前节点设置为其右节点,重复上述过程。

有左子树,遍历获得左子树最右边的节点(也就是前序遍历中,左子树最后一个访问的节点),

如果这个节点的right为空,将这个节点的right设置为当前节点root,访问root,将当前节点设置为root的左节点

如果该节点right已经有指向,说明这个节点已经被线索化了,将当前节点设置为root的右节点,设置right为null清除线索

[java] view plain copy
  1. /** 
  2.  * Definition for a binary tree node. 
  3.  * public class TreeNode { 
  4.  *     int val; 
  5.  *     TreeNode left; 
  6.  *     TreeNode right; 
  7.  *     TreeNode(int x) { val = x; } 
  8.  * } 
  9.  */  
  10. public class Solution {  
  11.     public List<Integer> preorderTraversal(TreeNode root) {  
  12.         List<Integer> res = new ArrayList<Integer>();  
  13.         if(root==nullreturn res;  
  14.         TreeNode cur = root;  
  15.         TreeNode pre = null;  
  16.         while(cur!=null){//当前节点不为空,也就是没有到最后  
  17.             if(cur.left==null){//如果当前节点左子树为空  
  18.                 res.add(cur.val);//访问当前节点  
  19.                 cur = cur.right;//设置其右节点为当前节点  
  20.             }else{//如果左子树不为空  
  21.                 pre = cur.left;  
  22.                 while(pre.right!=null&&pre.right!=cur){//获得左子树最右边的节点  
  23.                     pre = pre.right;  
  24.                 }  
  25.                 if(pre.right==null){//如果最右边节点没有被线索化  
  26.                     pre.right = cur;//线索化  
  27.                     res.add(cur.val);//访问当前节点  
  28.                     cur = cur.left;//设置其左节点为当前节点  
  29.                 }else{//如果被线索化了,说明这次访问,是为了访问其线索节点  
  30.                     pre.right = null;//取消线索化  
  31.                     cur = cur.right;//线索节点  
  32.                 }  
  33.             }  
  34.         }  
  35.         return res;  
  36.     }  
  37. }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46564215

原创粉丝点击