(Node root)
| 11 | } |
| 12 | |
| 13 | public static int convertToSumTree(Node root) { |
| 14 | if(root == null) { |
| 15 | return 0; |
| 16 | } |
| 17 | |
| 18 | int leftVal = convertToSumTree(root.left); |
| 19 | int rightVal = convertToSumTree(root.right); |
| 20 | int leftSubtree = root.left == null ? 0 : root.left.data; |
| 21 | int rightSubtree = root.right == null ? 0 : root.right.data; |
| 22 | |
| 23 | int myData = root.data; |
| 24 | root.data = leftSubtree + leftVal + rightSubtree + rightVal; |
| 25 | return myData; |
| 26 | } |
| 27 | |
| 28 | public static void preorder(Node root) { |
| 29 | if(root == null) { |