Give simple, common, and optimized Java solution. Given the root of a binary tree,…
- User
Give simple, common, and optimized Java solution. Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values. Two nodes of a binary tree are cousins if they have the same depth with different parents. Return the root of the modified tree. 返回修改后 root 树的根。 Note that the depth of a node is the number of edges in the path from the root node to it. 16 17 18 19 20 PITT class Solution { } public TreeNode replaceValueInTree(TreeNode root) {
Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values. Two nodes of a binary tree are cousins if they have the same depth with different parents. Return the root of the modified tree. 返回修改后 root 树的根。 Note that the depth of a node is the number of edges in the path from the root node to it. 16 17 18 19 20 PITT class Solution { } public TreeNode replaceValueInTree(TreeNode root) {
- Assistant