非遞歸遍歷二叉樹Java實現


題目:

要求使用非遞歸的方法,中序遍歷二叉樹。

 

解答:

 

前序遍歷

可以使用一個棧來模擬這種操作:

首先將root壓棧;

每次從堆棧中彈出棧頂元素,表示當前訪問的元素,對其進行打印;

依次判斷其右子樹,左子樹是否非空,並進行壓棧操作,至於為什么先壓棧右子樹,因為先壓棧的后彈出,左子樹需要先訪問,因此后壓棧;

中序遍歷和后序遍歷復雜一些。

  1 public class Solution {
  2 
  3     // 非遞歸前序遍歷
  4     public List<Integer> preorderTraversal(TreeNode root) {
  5         List<Integer> res = new ArrayList<>();
  6         if(root == null) {
  7             return res;
  8         }
  9 
 10         Stack<TreeNode> stack = new Stack<>();
 11         stack.push(root);
 12 
 13         while(!stack.isEmpty()) {
 14             TreeNode current = stack.pop();
 15             res.add(current.val);
 16 
 17             if(current.right != null) {
 18                 stack.push(current.right);
 19             }
 20 
 21             if(current.left != null) {
 22                 stack.push(current.left);
 23             }
 24         }
 25 
 26         return res;
 27     }
 28 
 29     // 非遞歸中序遍歷
 30     public List<Integer> inorderTraversal(TreeNode root) {
 31         List<Integer> res = new ArrayList<>();
 32         IF(root == null) {
 33             return res;
 34         }
 35 
 36         Stack<TreeNode> stack = new Stack<>();
 37 
 38         TreeNode p = root;
 39         while(p != null || !stack.isEmpty()) {
 40             if(p != null) {
 41                 stack.push(p);
 42                 p = p.left;
 43             } else {
 44                 p = stack.pop();
 45                 res.add(p.val);
 46                 p = p.right;
 47             }
 48         }
 49 
 50         return res;
 51     }
 52 
 53     // 非遞歸后序遍歷
 54     public List<Integer> postorderTraversal(TreeNode root) {
 55         List<Integer> res = new ArrayList<>();
 56         if(root == null) {
 57             return res;
 58         }
 59 
 60         Stack<TreeNode> stack = new Stack<>();
 61 
 62         TreeNode p = root;
 63 
 64         // 標記最近出棧的節點,用於判斷是否是p節點的右孩子,如果是的話,就可以訪問p節點
 65         TreeNode pre = p;
 66 
 67         while(p != null || !stack.isEmpty()) {
 68             if(p != null) {
 69 
 70                 stack.push(p);
 71                 p = p.left;
 72 
 73             } else {
 74                 p = stack.pop();
 75 
 76                 if(p.right == null || p.right == pre) {
 77                     res.add(p.val);
 78                     pre = cur;
 79                     p = null;
 80                 } else {
 81                     stack.push(p);
 82                     p = p.right;
 83                     stack.push(p);
 84                     p = p.left;
 85                 }
 86             }
 87         }
 88 
 89         return res;
 90     }
 91 
 92     // 非遞歸層次遍歷
 93     public List<Integer> levelTraversal(TreeNode root) {
 94         List<Integer> res = new ArrayList<>();
 95         if(root == null) {
 96             return res;
 97         }
 98 
 99         Queue<TreeNode> queue = new LinkedList<>();
100 
101         q.add(root);
102 
103         while(!queue.isEmpty()) {
104             // current node
105             TreeNode current = queue.remove();
106             res.add(current.val);
107 
108             if(current.left != null) {
109                 queue.add(current.left);
110             }
111 
112             if(current.right != null) {
113                 queue.add(current.right);
114             }
115         }
116 
117         return res;
118     }
119 
120     
121 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM