Advertisement
ogv

Untitled

ogv
Sep 13th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  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. class Solution {
  11.     public int minCameraCover(TreeNode root) {
  12.         if (root == null) return 0;
  13.         if (root.left == null && root.right == null) return 1;
  14.        
  15.         debugMark(root, 1);
  16.        
  17.         debugTree(root);
  18.        
  19.         TreeNode current;
  20.         LinkedList<TreeNode> path;
  21.        
  22.         for (int i = 0; i < 1; i++) {            
  23.             // descend from root the first time
  24.             current = root;
  25.             path = new LinkedList<>();
  26.            
  27.             while (current != null) {
  28.                 path.addLast(current);
  29.                 if (current.left != null) {
  30.                     current = current.left;
  31.                 }
  32.                 else if (current.right != null) {
  33.                     current = current.right;
  34.                 }
  35.                 else break;
  36.             }
  37.            
  38.             // enumerate all paths
  39.             for (;;) {
  40.                 debugPath(path);
  41.                
  42.                 // choose next path                
  43.                 while (path.size() >= 2) {                                        
  44.                     current = path.get(path.size()  - 2);
  45.                     if (current.left == path.getLast()) {                        
  46.                         path.removeLast();
  47.                         break;
  48.                     }
  49.                     path.removeLast();        
  50.                 }                
  51.                 // descend
  52.                 if (current == null) break;
  53.                
  54.                 current = current.right;
  55.                 while (current != null) {
  56.                     path.addLast(current);
  57.                     current = current.left;                    
  58.                 }
  59.             }
  60.         }
  61.    
  62.         return 0;
  63.     }
  64.  
  65.     private int debugMark(TreeNode root, int next) {
  66.         root.val = next++;
  67.         if (root.left != null) {
  68.             next = debugMark(root.left, next);
  69.         }
  70.         if (root.right != null) {
  71.             next = debugMark(root.right, next);
  72.         }
  73.         return next;
  74.     }
  75.  
  76.     private void debugTree(TreeNode root) {
  77.         System.out.print("Tree: ");
  78.         debugTreeInt(root);
  79.         System.out.println();
  80.     }
  81.  
  82.     private void debugTreeInt(TreeNode root) {
  83.         if (root == null) {
  84.             System.out.print(".");
  85.             return;
  86.         }
  87.         System.out.print(root.val);
  88.         System.out.print("L");
  89.         debugTreeInt(root.left);
  90.         System.out.print("R");
  91.         debugTreeInt(root.right);
  92.     }
  93.  
  94.     private void debugPath(LinkedList<TreeNode> path) {
  95.         System.out.print("Path: ");
  96.         for (TreeNode node: path) {
  97.             System.out.print(node.val + ", ");
  98.         }
  99.         System.out.println();        
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement